flo*_*olu 6 reactive-programming rxjs ngrx angular
我必须做同样的事情,虽然我更喜欢第一个.但第一种方法似乎不起作用.(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')
});
Run Code Online (Sandbox Code Playgroud)
ggr*_*nig 16
想象一下RxJS管道就像实际的物理管道一样,最后有一个阀门.每根管子都会"修改"流过它的液体,但只要末端的阀门关闭,就不会有任何流动.
所以,你需要的是在最后打开阀门.这是通过订阅可观察的管道来完成的.最简单的解决方案是:
this.actions$.pipe(
ofType(LayoutActions.Types.CHANGE_THEME),
takeUntil(this.destroyed$),
tap(() => {
console.log('test')
}),
).subscribe(_ => console.log("water is flowing!"));
Run Code Online (Sandbox Code Playgroud)
pipe创建新的,Observable因此您必须分配它,然后订阅该实例。在您的情况下,您将省略pipereturn,因此您最终会得到简单的、未修改的,Observable而无需任何额外的管道操作。
还请记住,您很可能必须订阅才能pipe(和tap)工作。
尝试
this.actions$=this.actions$.pipe(
tap(()=>console.log("First tap")),
ofType(LayoutActions.Types.CHANGE_THEME),
takeUntil(this.destroyed$),
tap(() => {
console.log('Last tap')
}),
);
this.actions$.subscribe(() => {
console.log('subscribtion')
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2706 次 |
| 最近记录: |