我使用 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,因此在角度/离子中没有设置它。 但是当我设置例如
map(([userData, dashboardData<any>]) => {
或类似的东西,它不起作用。我该如何解决这个问题?
getUser en 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;
此行的作用是将对象转换为打字稿的 any 类型。
更好的方法是为数据创建模型类,并使用这些模型类而不是任何模型类。
您可以像这样键入数组:
map(([userData, dashboardData]: [UserData, DashboardData]) =>
或者你可以只输入你的可观察量。不要滥用任何。