RxJS 5.5+中带有管道的多播运算符 [英] Multicast Operator with Pipe in RxJS 5.5+

查看:68
本文介绍了RxJS 5.5+中带有管道的多播运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在RxJS 5.5中使用 pipe()而不是链接运算符的新推荐方法,使​​用 multicast()运算符?当我像以前一样尝试使用 connect()时,出现TypeScript错误:

How do I use the multicast() operator with the new recommended approach in RxJS 5.5 of using pipe() instead of chaining operators? I get a TypeScript error when I try to use connect() like I did before:

const even$ = new Subject();

const connectedSub = interval(500)
    .pipe(
        filter(count => count % 2 === 0),
        take(5),
        multicast(even$)
    )
    .connect();

even$.subscribe(value => console.log(value));

此代码有效,但会产生TypeScript错误,该错误报告类型'Observable< {}>'上的属性'connect'不存在.我是否以应有的方式使用可连接的可观察对象在RxJS 5.5及更高版本中?

This code works but yields a TypeScript error that reports that Property 'connect' does not exist on type 'Observable<{}>'. Am I using connectable observables the way that I should be in RxJS 5.5+?

推荐答案

当前-v5.5.10和v6.1.0- pipe 的类型不知道 Observable 子类,因此我使用类型断言,如下所示:

The current - v5.5.10 and v6.1.0 - typings for pipe are not aware of the Observable subclasses, so I use a type assertion, like this:

const connectedObs = interval(500).pipe(
    filter(count => count % 2 === 0),
    take(5),
    multicast(even$)
) as ConnectableObservable<number>;
const connectedSub = connectedObs.connect();

这篇关于RxJS 5.5+中带有管道的多播运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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