问题
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));
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+?
回答1:
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();
来源:https://stackoverflow.com/questions/50166366/multicast-operator-with-pipe-in-rxjs-5-5