更新时间:2022-11-30 11:57:45
我认为您的问题位于此处:
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
.
或者你可以使用 bind 方法保留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()
.