在Angular应用中,我使用HttpClient和RxJS运算符进行一些API调用。例如:
return this.httpClient.put(url, JSON.stringify(treeOptions))
.pipe(
map(this.extractTreeData),
catchError(this.handleError)
);
Run Code Online (Sandbox Code Playgroud)
该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 || {};
}
Run Code Online (Sandbox Code Playgroud)
我需要根据树的类型进行更多更改。如何将这种类型传递给映射方法?我的意思是:
[...]
.pipe(
map(this.extractTreeData), <== HOW TO PASS VARIABLE 'treeType' TO extractTreeData
[...]
Run Code Online (Sandbox Code Playgroud)
提前致谢,
线
map(this.extractTreeData)
等于
map(tree => this.extractTreeData(tree))
如果使用扩展形式,则可以对其进行更新以传递更多参数,如下所示:
map(tree => this.extractTreeData(tree, treeType))
尽管您没有显示出哪里treeType来,但是很难给您一个完整的答案。
| 归档时间: |
|
| 查看次数: |
321 次 |
| 最近记录: |