我试图按照教程here进行操作,但由于我使用的是较新版本的Angular和Ionic(Angular 5和Ionic 3),我在下面的这些行中出错了
this.token = data.token;
this.storage.set('token', data.token);
Visual Studio代码显示此错误:
Property token does not exist on type 'Object'
该功能的代码如下:
createAccount(details){
return new Promise((resolve, reject) => {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
this.http.post('https://YOUR_HEROKU_APP.herokuapp.com/api/auth/register', JSON.stringify(details), {headers: headers})
.subscribe(res => {
let data = res.json();
this.token = data.token;
this.storage.set('token', data.token);
resolve(data);
}, (err) => {
reject(err);
});
});
}
我的代码应该更改什么?
答案 0 :(得分:7)
尝试将其解析为,
this.token = data['token'];
this.storage.set('token', this.token);
答案 1 :(得分:1)
如果Typescript无法识别对象“ res”上的属性“ token”,则更改为res:any
this.http.post(api.url, params)
.subscribe((res:any) => {
...
..
}