作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将可观察量与 RXJS 库一起使用,并且遇到一种情况,我想提取一个值并在未来某个未指定的时间(不是下一个运算符或订阅方法)使用它。
我目前正在通过每个操作传递该值,直到我需要它为止。看起来像这样的东西:
obsveable$.pipe(
ofType('example'),
map((action) => [
action, // <-- passing this through
somethingElse
]),
map(([action, other]) => [
action, // <-- through again
anotherThing
]),
map(([action, other]) => {
// finally use action
}),
);
我也许可以尝试将其保存到局部变量中 - 像这样:
let action;
obsveable$.pipe(
ofType('example'),
map((_action) => action = _action),
map((action) => action.somethingElse),
map((other) => {
// finally use action
})
);
这两种解决方案都不是最优的。当有很多步骤时,第一个会变得笨拙,第二个可能会出现操作顺序问题(并且它没有使用很酷的 react 方法)。
还有哪些其他选择?
最佳答案
您可以通过以下方式创建一个闭包:
obsveable$.pipe(
ofType('example'),
mergeMap(action =>
Observable.of(somethingElse)
.pipe(
map(other => anotherThing),
map(other => {
// use action
})
)
)
);
关于javascript - 用于稍后提取值的可观察运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49212159/
我是一名优秀的程序员,十分优秀!