从 Es5 中的 Angular2 http 获取错误
Get an error from Angular2 http in Es5
我正在尝试将 http 与 Angular2 一起使用。
这是我的代码:
var _domain = 'http://localhost:3000/';
app.Applications = ng.core.Injectable().Class({
constructor: [ng.http.Http, function(http) {
this.http = http;
this.emailExistUrl = _domain + 'api/applications/email';
}],
doesEmailExist: function(email) {
var data = { email: email };
return this.http.post(this.emailExistUrl, data)
.toPromise()
.then(function(response) { response.json().data; })
.catch(this.handleError);
}
});
主要成分:
app.AppComponent = ng.core
.Component({
selector: 'register-form',
templateUrl: 'src/register/app.component.html',
providers: [app.Applications]
})
.Class({
constructor: [ng.core.ElementRef, app.Applications, function(ref, Applications) {
this.programs = JSON.parse(ref.nativeElement.getAttribute('programs'));
this.applications = Applications;
}],
doesEmailExist: function(email) {
return this.applications.doesEmailExist(email);
}
});
这里是 main.js 文件:
document.addEventListener('DOMContentLoaded', function() {
ng.platformBrowserDynamic.bootstrap(app.AppComponent, [
ng.forms.disableDeprecatedForms(),
ng.forms.provideForms(),
ng.http.HTTP_PROVIDERS,
]);
});
调用 doesEmailExist 时,http 模块出现错误:
vendor-client.min.js:55470 TypeError: 无法读取未定义的 属性 'platform_browser_private'
有什么想法吗?
已修复:
Http 在脚本标签列表中位于 platform-browser 之前。 :/
<script src="https://npmcdn.com/@angular/http/bundles/http.umd.js"></script>
<script src="https://npmcdn.com/@angular/platform-browser/bundles/platform-browser.umd.js"></script>
<script src="https://npmcdn.com/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js"></script>
倒数更好:)
尝试在构造函数的开头赋值http
:
app.Applications = ng.core.Injectable().Class({
constructor: [ng.http.Http, function(http) {
this.http = http;
...
}],
doesEmailExist: function(email) {
...
}
});
编辑
看到这个 Plunker:http://plnkr.co/edit/aQWqxauklT7MqSjfhLFD。为了简化,我将所有内容都放在 main.js
文件中,而不是 http post 我实现了 http get。但是,在本地,即使是 http post 也可以使用 Web 服务 API。希望对解决您的问题有所帮助。
我正在尝试将 http 与 Angular2 一起使用。 这是我的代码:
var _domain = 'http://localhost:3000/';
app.Applications = ng.core.Injectable().Class({
constructor: [ng.http.Http, function(http) {
this.http = http;
this.emailExistUrl = _domain + 'api/applications/email';
}],
doesEmailExist: function(email) {
var data = { email: email };
return this.http.post(this.emailExistUrl, data)
.toPromise()
.then(function(response) { response.json().data; })
.catch(this.handleError);
}
});
主要成分:
app.AppComponent = ng.core
.Component({
selector: 'register-form',
templateUrl: 'src/register/app.component.html',
providers: [app.Applications]
})
.Class({
constructor: [ng.core.ElementRef, app.Applications, function(ref, Applications) {
this.programs = JSON.parse(ref.nativeElement.getAttribute('programs'));
this.applications = Applications;
}],
doesEmailExist: function(email) {
return this.applications.doesEmailExist(email);
}
});
这里是 main.js 文件:
document.addEventListener('DOMContentLoaded', function() {
ng.platformBrowserDynamic.bootstrap(app.AppComponent, [
ng.forms.disableDeprecatedForms(),
ng.forms.provideForms(),
ng.http.HTTP_PROVIDERS,
]);
});
调用 doesEmailExist 时,http 模块出现错误:
vendor-client.min.js:55470 TypeError: 无法读取未定义的 属性 'platform_browser_private'
有什么想法吗?
已修复: Http 在脚本标签列表中位于 platform-browser 之前。 :/
<script src="https://npmcdn.com/@angular/http/bundles/http.umd.js"></script>
<script src="https://npmcdn.com/@angular/platform-browser/bundles/platform-browser.umd.js"></script>
<script src="https://npmcdn.com/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js"></script>
倒数更好:)
尝试在构造函数的开头赋值http
:
app.Applications = ng.core.Injectable().Class({
constructor: [ng.http.Http, function(http) {
this.http = http;
...
}],
doesEmailExist: function(email) {
...
}
});
编辑
看到这个 Plunker:http://plnkr.co/edit/aQWqxauklT7MqSjfhLFD。为了简化,我将所有内容都放在 main.js
文件中,而不是 http post 我实现了 http get。但是,在本地,即使是 http post 也可以使用 Web 服务 API。希望对解决您的问题有所帮助。