angular - RXJS 管道中未触发 tap()
问题描述
我必须做同样的事情的方法,虽然我更喜欢第一个。但是第一种方法似乎不起作用。(tap()
未触发)
// does not work
this.actions$.pipe(
ofType(LayoutActions.Types.CHANGE_THEME),
takeUntil(this.destroyed$),
tap(() => {
console.log('test')
}),
);
// works
this.actions$.ofType(LayoutActions.Types.CHANGE_THEME).subscribe(() => {
console.log('test')
});
解决方案
想象一下 RxJS 管道就像实际的物理管道,末端有一个阀门。每条管道都会“改变”流过它的液体,但只要末端的阀门关闭,就不会有任何东西流动。
所以,你需要的是在最后打开阀门。这是通过订阅可观察管道来完成的。最简单的解决方案是:
this.actions$.pipe(
ofType(LayoutActions.Types.CHANGE_THEME),
takeUntil(this.destroyed$),
tap(() => {
console.log('test')
}),
).subscribe(_ => console.log("water is flowing!"));
推荐阅读
- html - 我找不到导致水平滚动的原因
- reactjs - 查询修剪不是函数(如何调试)
- sql - SQL 查询 - 如何输出重复值
- css - 为什么 SASS @each 语句在 Next.js 中不起作用?
- r - 将日期时间变量离散化为“in-hours”和“after-hours”
- r - 如何根据空白行从 df 分区到多个 .csv?
- c++ - Pytorch C++ API:CMake 问题
- java - JDBI -> 将 MySql DATETIME 读入 java.sql.Date 或类似的
- dart - Dart 是否具有尾调用优化 (TCO) 功能?
- python - 如何使用自定义内核导出 SVM 模型?