簡體   English   中英

Typescript 中的 Object 類型不存在屬性

[英]Property does not exist on type Object in Typescript

我正在使用 Ionic 和 Angular2 Typescript。 呈現視圖時,我在文件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);
        });
    });
}

嘗試通過以下方式繞過錯誤:

this._notifications = data['notifications'];

嘗試另一種方法來繞過錯誤:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM