RXJS 管道中未触发 tap() [英] tap() isn't triggered in RXJS Pipe

查看:20
本文介绍了RXJS 管道中未触发 tap()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须有办法做同样的事情,虽然我更喜欢第一个.但是第一种方法似乎不起作用.(tap() 没有被触发)

I have to ways of doing the same thing, although I prefer the first one. But the first approach doesn't seem to work. (the tap() is not triggered)

// 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 管道想象成实际的、末端带有阀门的物理管道.每根管道都会改变"流经它的液体,但只要末端的阀门关闭,任何东西都不会流动.

Imagine RxJS pipes like actual, physical pipes with a valve at the end. Each pipe will "modify" the liquid that is flowing through it, but as long as the valve at the end is closed, nothing will ever flow.

所以,你需要的是最后打开阀门.这是通过订阅可观察管道来完成的.最简单的解决方案是:

So, what you need, is to open the valve at the end. This is done by subscribing to the observable pipe. The easiest solution is:

this.actions$.pipe(
    ofType(LayoutActions.Types.CHANGE_THEME),
    takeUntil(this.destroyed$),
    tap(() => {
        console.log('test')
    }),
).subscribe(_ => console.log("water is flowing!"));

这篇关于RXJS 管道中未触发 tap()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆