繁体   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