Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rxjs 6 equivalent of Observable.create(subscriber -> {...}).share()

I'm upgrading my Angular 5 app to Angular 6 and consequently from rxjs 5 to rxjs 6, I'm experiencing troubles in migrating the following piece of code:

const myObservable = Observable.create(subscriber => {
    // do something with the subscriber
}).share();

in particular I'm getting this error:

TypeError: Observable_1.Observable.create(...).share is not a functionTypeError: Observable_1.Observable.create(...).share is not

like image 616
Francesco Borzi Avatar asked Jun 19 '18 13:06

Francesco Borzi


2 Answers

You need to pipe share() as follows instead of chaining:

const myObservable = Observable.create(subscriber => {
    // do something with the subscriber
}).pipe(share());

Also make sure you import share as follows:

import {share} from 'rxjs/operators';
like image 174
siva636 Avatar answered Nov 18 '22 11:11

siva636


import { Observable } from "rxjs";
...
let obs$ = new Observable(...);
...

Above code should do the trick

like image 45
Abinesh Devadas Avatar answered Nov 18 '22 11:11

Abinesh Devadas