Search code examples
angularrxjsrxjs6

RxJs 6: Get ConnectableObservable from Observable


Angular 6 requires an update to RxJs 6 and with that RxJs update the Observable.publish() function is gone. I found a publish operator in RxJs/operators but I'm having trouble figuring out how to use it.

How could this RxJs 5 code be rewritten to work with RxJs 6?

const myConnectableObservable = this.getObservable().publish()


Solution

  • import { ConnectableObservable } from "rxjs"
    import { publish } from "rxjs/operators";
    
    const myConnectableObservable: ConnectableObservable<MyClass> = myService.getObservable().pipe(publish()) as ConnectableObservable<MyClass>;
    

    Special thanks to @cartant