【问题标题】:How to pass extra parameters to RxJS map operator如何将额外参数传递给 RxJS 映射运算符
【发布时间】:2018-11-27 13:13:41
【问题描述】:

在 Angular 应用程序中,我使用 HttpClient 和 RxJS 运算符进行一些 API 调用。例如:

return this.httpClient.put(url, JSON.stringify(treeOptions))
        .pipe(
        map(this.extractTreeData),
        catchError(this.handleError)
        );

extractTreeData 方法基本上改变了 API 返回的一些属性:

private extractTreeData(res: any) {
    let apiTree = res.data;  // The tree comes inside the 'data' field
    let newTree : any;

    try {
        let str = JSON.stringify(apiTree);

        str = str.replace(/"children"/g, '"items"');
        str = str.replace(/"allowdrag"/g, '"allowDrag"');

        newTree = JSON.parse(str);        
    } catch(e) {
        // Error while parsing
        newTree = null;
    }

    return newTree || {};
}

我需要根据树的类型进行更多更改。我怎样才能将这种类型传递给映射方法?我的意思是:

[...]
.pipe(
  map(this.extractTreeData), <== HOW TO PASS VARIABLE 'treeType' TO extractTreeData
[...]

提前致谢,

【问题讨论】:

    标签: angular rxjs


    【解决方案1】:

    线

    map(this.extractTreeData)

    相当于

    map(tree =&gt; this.extractTreeData(tree))

    如果您使用扩展表单,则可以更新它以传递更多参数,如下所示:

    map(tree =&gt; this.extractTreeData(tree, treeType))

    虽然你没有显示treeType 的来源,但很难给你一个完整的答案。

    【讨论】:

      猜你喜欢
      • 2014-01-19
      • 1970-01-01
      • 1970-01-01
      • 2020-03-15
      • 2018-10-07
      • 1970-01-01
      • 2021-07-28
      • 2011-03-06
      • 2021-02-27
      相关资源
      最近更新 更多