I'm creating an angular2 app and I've got a service that I want to be able to write to in one component and then read/update it in other components (routes).
Here's what my service looks like currently:
import { Injectable } from '@angular/core';
import { Filer } from './filer.model';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class FilerService {
public _filer: Subject<Filer>;
constructor() {
this._filer = new Subject();
}
setSelected(ret: Filer) {
let temp = ret;
this._filer.next(temp);
}
get selected(): Observable<Filer> {
return this._filer.asObservable();
}
}
since I'm using Subject
, only components that are subscribed will be able to see any values. I tried changing it to a BehaviorSubject
, based on the rxjs docs. Here's what that looks like:
import { Injectable } from '@angular/core';
import { Filer } from './filer.model';
import { Observable } from 'rxjs/Observable';
import { Observer } from 'rxjs/Observer';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export class FilerService {
public _filer: BehaviorSubject<Filer>;
constructor() {
this._filer = new BehaviorSubject({});
}
setSelected(ret: Filer) {
let temp = ret;
this._filer.onNext(temp);
}
get selected(): Observable<Filer> {
return this._filer.asObservable();
}
}
However, I get Property 'onNext' does not exist on type 'BehaviorSubject<Filer>'.
, so I'm not sure where to go from here. Any ideas?
I'm on rxjs 5.0.0-beta.6.
If you are using the latest angular2 then it uses rxjs 5, in which case you have to use next()
and not onNext()
.
Have a look at the migration guide .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.