use*_*579 0 http typescript angular
我试图通过promises和http.get处理登录,但我失败了,我得到以下错误:
对象不支持属性或方法'toPromise'
我的代码是:
return this.http.get('http://localhost:5000/login/', {
headers: authHeader
}).map((response) => {
return response.json()
}).toPromise(null);
Run Code Online (Sandbox Code Playgroud)
我得到了:
更新:
JohnPapa将他的项目更新为我的朋友
https://github.com/johnpapa/angular2-there-and-back-again/blob/master/app/core/character.service.ts
我想知道你是否真的使用了promise,因为Angular的HTTP支持依赖于Observables.
要获得响应,您只需要为您的调用返回observable:
getSomething() {
return this.http.get('http://localhost:5000/login/', {
headers: authHeader
}).map((response) => {
return response.json()
})
}
Run Code Online (Sandbox Code Playgroud)
调用该方法时,您可以使用以下subscribe方法注册回调:
getSomething().subscribe(
data => handleData(data),
err => reject(err));
Run Code Online (Sandbox Code Playgroud)
如果你真的想使用promises(使用toPromise方法),你应该导入:
import 'rxjs/Rx';
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请参阅此问题:https://github.com/angular/angular/issues/5632#issuecomment-167026172.
否则,FYI调用与浏览器中的HTTP不同步...
希望它对你有帮助,蒂埃里