更新时间:2022-11-30 11:01:55
我认为您的问题位于此处:
I think that your problem is located here:
getRisks(): Observable<RiskListSummary[]> {
return this.http.get(this.serviceUrl)
.map(this.extractData()) <== passing result of function
.catch(this.handleError()); <== passing result of function
}
您可以使用仅传递function
参考:
You could use just passing function
reference:
getRisks(): Observable<RiskListSummary[]> {
return this.http.get(this.serviceUrl)
.map(this.extractData)
.catch(this.handleError);
}
但是这样一来,您将失去this
.
but this way you will lose this
.
或者您可以使用绑定保留this
的方法:
Or you could use bind method to retain this
:
getRisks(): Observable<RiskListSummary[]> {
return this.http.get(this.serviceUrl)
.map(this.extractData.bind(this))
.catch(this.handleError.bind(this));
}
但是您将丢失类型检查.
I would leverage arrow functions to be able to use lexical this:
getRisks(): Observable<RiskListSummary[]> {
return this.http.get(this.serviceUrl)
.map(res => this.extractData(res))
.catch(err => this.handleError(err));
}
没有它,this
变量将指向进行调用的函数,而不是包含getRisks()
的实例.
Without it, the this
variable will point to the function where the call is made, instead of the instance that contains getRisks()
.