【问题标题】:Adding more statements in observable Map在 observable Map 中添加更多语句
【发布时间】:2018-01-30 08:13:48
【问题描述】:

这里我有一个工作正常的方法,能够订阅结果。

fetchIncidentDetails(incidentId: number): Observable<any> {
    const url = `${AppUtils.INCIDENT_SEARCH}/${incidentId}/${+localStorage.getItem(AppUtils.PERSON_CODE)}`;
    let incident: Observable<IncidentReportModel>; 
    incident= this.http.get(url)
        .map(response => response.json().result)
        .catch(this.handleError);

    return incident;
}


public setIncidents(incidents: IncidentReportModel) {
    this.incidents = incidents;
}

如果我在地图中再添加一条语句,则无法订阅响应。 我需要设置响应值。我怎样才能做到这一点。

.map(response =>{response.json().result;  this.setIncidents(response.json().result)})})

【问题讨论】:

  • 你遇到了什么错误?
  • 当您添加额外的语句时,您不再返回任何内容。您基本上是将 Observable 更改为 Observable.

标签: angular typescript ecmascript-6 rxjs


【解决方案1】:

我怎么理解你想做这样的事情

fetchIncidentDetails(incidentId: number): Observable<any> {
    const url = `${AppUtils.INCIDENT_SEARCH}/${incidentId}/${+localStorage.getItem(AppUtils.PERSON_CODE)}`;
    let incident: Observable<IncidentReportModel>; 
    incident= this.http.get(url)
        .map(response => {
            this.setIncidents(response.json().result)
            return response.json().result
             })
        .catch(this.handleError);

    return incident;
}

了解arrow function 在 java 脚本中的工作原理

(param1, param2, …, paramN) => expression
// equivalent to: (param1, param2, …, paramN) => { return expression; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多