我使用 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/