传递/保留长管输掉值的理智方法 [英] sane way to pass/keep a value throwout a long pipe
本文介绍了传递/保留长管输掉值的理智方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有以下rxjs管道:
Assuming i have the following rxjs pipe:
start$.pip(
map((id)=> {}), //I want to save the "id" value to be used in the end of the pipe
map(...),
switchMap(...),
map(...),
switchMap(...),
map(...),
switchMap(...),
switchMap(...)//I need the original "id" value here
).subscribe()
是否有一种方法可以在整个点子中保持"id"值,以便可以在管道末端使用它?
Is there a way to keep the 'id" value throughout the pip so it can be used at the end of the pipe?
动机:通常是在NGRX效果中,我想使用触发源操作的原始有效载荷数据来生成新操作.
Motivation: It comes often in NGRX effects where i want to use the original payload data of the triggering source action for generating the new action.
推荐答案
我认为正确的方法是进行另一个关闭
I think the correct way is to make another closure
const processId = (id) => observableOf(id)
.pipe(
map(() => { ... }),
map(...),
switchMap(...),
map(...),
switchMap(...),
map(...),
switchMap(...),
switchMap(...) // Use id here
);
const getPipe = () => start$
.pipe(switchMap(processId));
将局部变量作为副作用存储在getPipe
中是可以的,但是如果start$
Observable发出更多的值,则它可能会中断.
Storing local variable as a side effect in getPipe
is OK, but it can break if start$
Observable emits more values.
这篇关于传递/保留长管输掉值的理智方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文