从 Angular 2 升级到 Angular 4 后,尝试使用 AOT 进行编译时出现以下错误:
Property 'total' does not exist on type 'Observable<any>'.
这就是我到目前为止使用 observables 的方式......
首先,我分配了一个这样的变量:
public myData: Observable<any>;
然后我打电话给我订阅的服务,如下所示:
this.mySubscription = this.apiService.get('URL-HERE')
.subscribe(
response => this.myData = response,
error => this.feedbackService.displayError(<any>error),
() => this.feedbackService.stopLoading()
);
这适用于开发模式(ng serve),我可以使用诸如*ngFor
迭代结果之类的东西。
收到这些错误后,我尝试添加这样的模型类:
export class MyData {
total: number;
count: number;
data: Array<any> = [];
}
... rest of component here
然后将它与我的 observable 一起使用,如下所示:
public myData: Observable<MyData>;
但是我在尝试编译时仍然遇到错误:
Property 'total' does not exist on type 'Observable<MyData>'.
我究竟做错了什么?
更新:
这是服务方法的样子:
return this.http
.get(apiUrl + apiPath, {headers: headersWithToken})
.timeoutWith(appSettings.maxTimeHttpCalls, Observable.defer(() => Observable.throw(this.feedbackService.timeout())))
.map((response: Response) => {
return response.json();
})
.catch((err: Response) => {
return err.json();
});
所有值(包括total
)都存在于返回的 JSON 中,我可以在我的 HTML 中毫无问题地使用它。