如何将额外的参数传递给RxJS映射运算符 [英] How to pass extra parameters to RxJS map operator
问题描述
在Angular应用中,我使用 HttpClient
和RxJS运算符进行一些API调用.例如:
In an Angular app, I use HttpClient
and RxJS operators to make some API calls. For example:
return this.httpClient.put(url, JSON.stringify(treeOptions))
.pipe(
map(this.extractTreeData),
catchError(this.handleError)
);
extractTreeData
方法基本上更改了API返回的一些属性:
The extractTreeData
method basically changes some properties returned by the 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 || {};
}
我需要根据树的类型进行更多更改.如何将这种类型传递给映射方法?我的意思是:
I need to do some more changes depending on the type of tree. How could I pass this type to the mapped method? I mean:
[...]
.pipe(
map(this.extractTreeData), <== HOW TO PASS VARIABLE 'treeType' TO extractTreeData
[...]
预先感谢
推荐答案
行
map(this.extractTreeData)
等同于
map(tree => this.extractTreeData(tree))
如果使用扩展形式,则可以对其进行更新以传递更多参数,如下所示:
If you use the expanded form, you can then update it to pass more parameters as follows:
map(tree => this.extractTreeData(tree,treeType))
尽管您没有显示 treeType
的来源,但是很难给您完整的答案.
It is hard to give you a full answer though as you don't show where treeType
is coming from.
这篇关于如何将额外的参数传递给RxJS映射运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!