【发布时间】:2017-01-12 16:16:58
【问题描述】:
我可以阅读 rxjs 文档 (http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-scan) 中的方法 scan 有一个种子争论。
在下面的代码中,我想为 _channels Observables 返回一个默认值。
export interface IChannelOperation extends Function {
(channels: Channel[]): Channel[];
}
let initialChannel: Channel[] = [];
@Injectable()
export class ChannelsCatalog{
defaultChannel: Subject<Channel> = new BehaviorSubject<Channel>(null);
private _currentChannel: Subject<Channel> = new BehaviorSubject<Channel>(null);
private _channels: Observable<Channel[]>;
private _updates: Subject<any> = new Subject<any>();
constructor(){
this._channels = this._updates
.scan((channels: Channel[], operation: IChannelOperation) => operation(channels), initialChannel)
.publishReplay(1)
.refCount();
}
getAllChannelsCatalog(): Observable<Channel[]>{
return this._channels;
}
}
但是当我订阅可观察的 ex 时,种子参数没有返回:
var channelsCatalog = new ChannelsCatolog();
channelsCatalog.getAllChannelsCatalog.subscribe((value) => console.log(value));
【问题讨论】:
-
你可能想看看
startWith()也许会有所帮助。
标签: rxjs observable