After updating to RxJs 5 I am getting the following error:
share is not a function
Code:
import { Observable } from 'rxjs/Observable';
import { Observer } from 'rxjs/Observer';
import { Injectable } from '@angular/core';
import { share } from "rxjs/operators/share";
import * as m from '../../models/app.models';
@Injectable()
export class SharedService {
observable: Observable<any>;
observer: Observer<any>;
constructor() {
var temp = Observable.create((observer: Observer<any>) => {
this.observer = observer;
});
this.observable = temp.share(); <- Error here
}
broadcast(event: m.SharedEventModel) {
this.observer.next(event);
}
on(eventName, callback) {
return this.observable.filter((event) => {
return event.Name === eventName;
}).subscribe(callback);
}
}
Trying to make a globalEvents following http://stackoverflow.com/a/36174340/3955513
RxJs Version: 5.5.2
This is the working service.
I've updated Thierry Templier's plunker from the question you're using as a reference, Plunker
Note, import { share } from "rxjs/operators";
not import { share } from "rxjs/operator/share";
as stated elsewhere. See lettable-operators.md
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Observer } from 'rxjs/Observer';
import { share } from "rxjs/operators"; // lettable operator, tree-shakeable
import { filter } from "rxjs/operators";
@Injectable()
export class SharedService {
observable: Observable<any>;
observer: Observer<any>;
constructor() {
var temp = Observable.create((observer: Observer<any>) => {
this.observer = observer;
});
this.observable = temp.pipe(share());
}
broadcast(event) {
this.observer.next(event);
}
on(eventName, callback) {
this.observable.pipe(filter(x => x.name === eventName))
.subscribe(callback);
}
}
You're importing lettable "share()
" operator but trying to use it as the original "patch" operators.
You should use it as follows:
import { share } from "rxjs/operator/share";
...
this.observable = share(temp);
Or like this:
import { share } from "rxjs/operators/share";
...
this.observable = temp.pipe(share());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With