1
我正在嘗試使用http和Angular2。 這裏是我的代碼:在Es5中獲取Angular2 http的錯誤
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'的undefined
任何想法?
修復: 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>
逆更好:)
仍然是一樣的錯誤 – user2705463
對不起,我把'doesEmailExist'方法與'emailExistUrl'混淆了。你在哪裏叫這個方法? – robisim74
來自主要組件。應用程序服務注入主要組件。它也有一個doesEmailExist方法:return this.applications.doesEmailExist(email); – user2705463