属性不存在TypeScript中的对象类型

属性不存在TypeScript中的对象类型

问题描述:

所以我使用Ionic和Angular2打字稿。当查看被渲染我调用函数retrieveNotifications文件dashboard.ts,但我得到一个错误:属性不存在TypeScript中的对象类型

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

然后在文件中的用户,data.ts这声明为公共用户: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); 
      }); 
     }); 
    } 
+1

http://stackoverflow.com/questions/40510795/cannot-call-a-function-上的元件,内部-ngfor-angular2/40511035#40511035 – silentsod

尝试绕过与错误:

this._notifications = data['notifications']; 

尝试另一种方式与绕过错误:

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