带有RxJS 5.5+管道的组播运营商

时间:2018-05-04 02:40:32

标签: javascript angular typescript rxjs

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

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错误报告Property 'connect' does not exist on type 'Observable<{}>'.我是否使用可连接的observables,就像我应该在RxJS 5.5 +中那样?

1 个答案:

答案 0 :(得分:4)

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

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