且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

可观察< {}>不可分配给Observable< SomeType []>类型;

更新时间: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().