【问题标题】:Property does not exist on type Object in TypescriptTypescript 中的类型 Object 上不存在属性
【发布时间】: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


【解决方案1】:

尝试绕过错误:

this._notifications = data['notifications'];

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 2018-08-17
      • 1970-01-01
      • 2023-02-06
      • 2021-01-03
      • 1970-01-01
      • 2019-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多