【发布时间】:2020-05-13 20:45:40
【问题描述】:
我正在使用 Ionic 和 Angular2 打字稿。渲染视图时,我在文件 dashboard.ts 中调用函数 retrieveNotifications,但出现错误:
Error: Error at /home/invicta/Desktop/newauth/.tmp/pages/dashboard/dashboard.ts:36:34 Property 'notifications' does not exist on type '{}'
//dashboard.ts
ionViewDidEnter() {
this.retrieveNotifications();
}
retrieveNotifications() {
this.user.retrieveNotifications().then(data = > {
// when do console.log here and typeof(), data is object and it has notifications array
this._notifications = data.notifications;
})
}
然后在文件 user-data.ts 中声明为 public user: UserData 是函数:
//user-data.ts -> provider
retrieveNotifications() {
var token = this.authservice.getUserToken();
return new Promise(resolve = > {
var headers = new Headers();
headers.append('Authorization', 'Bearer ' + token);
this.http.get(this.mainUrl + '/api/v1/notification', {
headers: headers
}).subscribe(data = > {
if (data.status === 200)
resolve(data.json());
else
resolve(false);
});
});
}
【问题讨论】:
标签: angular typescript promise ionic2