【发布时间】:2017-06-30 09:34:31
【问题描述】:
我正在使用 .share() 在服务的所有订阅者之间共享一个 Observable:
@Injectable()
export class ChannelsService {
private store: IChannel[] = [];
private _own: BehaviorSubject<IChannel[]> = new BehaviorSubject([]);
readonly own: Observable<IChannel[]> = this._own.asObservable().share();
...(the rest of the service basically makes CRUD http request and then calls this._own.next(result) to emit the result to all subscribers)
}
问题: 只有对 Observable (ChannelsService.own.subscribe(...)) 的第一个订阅正在获取初始数据,其余订阅将“null”作为第一个订阅的值。对 this._own.next(result) 的下一次调用将正确地将其值发送给所有订阅者。
知道如何在多个订阅者之间共享()一个 Observable 并获取为所有订阅者发出的最后一个值吗? (我试过 .share().last() 但没办法...)。
谢谢!
【问题讨论】:
标签: angular rxjs observable angular2-services