在Typescript中的Object类型中不存在属性

时间:2016-11-09 18:31:56

标签: angular typescript promise ionic2

所以我使用Ionic和Angular2打字稿。呈现View时,我在文件retrieveNotifications中调用函数dashboard.ts,但是出现错误:

Error: Error at /home/invicta/Desktop/newauth/.tmp/pages/dashboard/dashboard.ts:36:34 
Property 'notifications' does not exist on type '{}'

以下是截图:https://www.dropbox.com/s/6x2myxqm2q4dhzb/Screenshot%202016-11-09%2019.29.52.png?dl=0

//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是function:

//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);
            });
        });
    }

2 个答案:

答案 0 :(得分:10)

尝试使用以下方法绕过错误:

this._notifications = data['notifications'];

答案 1 :(得分:0)

尝试另一种方法绕过错误:

return new Promise().then(()=>{
     return data;
})