angular - 订阅的对象类型上不存在属性

标签 angular typescript ionic4 jslint

我使用 forkJoin 进行多个 http 调用,但它给我错误 error TS2339: Property 'data' does not exist on type 'Object'

forkJoin(this.userservice.getUser(), this.userservice.getDashboard()).pipe(
    map(([userData, dashboardData]) => {

      // set the user
      this.user = userData;

      // set order count
      this.orderCount.new = dashboardData.data.new.length;
      console.log(dashboardData);

      this.dataLoaded = true;
    })
).subscribe();

我理解这个错误,因为这个属性来自外部 api,所以在 angular/ionic 中它没有被设置。但是当我设置例如

map(([userData, dashboardData<any>]) => {

或类似的东西,它不起作用。我该如何解决这个问题?

getUser 和 getDashboard 返回 http 对象

    getUser() {
    return this.http.get(environment.baseUrl + '/auth/user').pipe(
        map(results => {
            console.log(results);
            return results;
        })
    );
}

最佳答案

在你的代码中,替换这一行

  this.orderCount.new = dashboardData.data.new.length;

有了这个

  this.orderCount.new = (dashboardData as any).data.new.length;

这一行所做的是将 Object 转换为 typescript 的任何类型。

更好的方法是为数据创建模型类并使用这些模型类而不是任何模型类。

关于angular - 订阅的对象类型上不存在属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57531038/

相关文章:

javascript - 为什么 Kyle Simpson 的 OLOO 方法感觉像是与 Typescript 潮流背道而驰?

node.js - 如何将我的 ionic 应用程序连接到 cpanel Node 服务器?

ionic-framework - 不可能使用 ionic 载玻片方法(ChangeDetectorRef 没有提供者)

ionic-framework - Ionic 4 中的深层链接如何工作?

asynchronous - 如何在 typescript 中使用异步/等待

javascript - 在 setInterval 中调用函数

angular - 给占位符在 Angular 6 中选择

angular - 如何在 Angular 中的嵌套模块上进行路由?

javascript - 将 fetchPolicy 设置为 "standby"时无法调用 client.watchQuery - 从 useLazyQuery 加载页面时引发错误

javascript - 使用 Angular 2 接口(interface)