我正在尝试将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,
]);
});
当调用DeMeaLebug时,我从HTTP模块中得到一个错误:
vendor client.min.js:55470 typeerror:无法读取未定义的“platform_browser_private”属性
有什么想法吗?
固定的:
在脚本标记列表中,http位于平台浏览器之前。:。/
<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) {
...
}
});
编辑
看看这个强盗:http://plnkr.co/edit/aQWqxauklT7MqSjfhLFD。为了简化,我把所有东西都放在
main.js
文件中,并且实现了一个http get,而不是一个http post。然而,在本地,甚至http post也可以使用web服务api。我希望这对解决你的问题有帮助。关于http - 在Es5中从Angular2 http中获取错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38779982/