.asObservable 不想与 Observable.forkJoin 一起工作
.asObservable dont want to work with Observable.forkJoin
我有服务:
export class ConfigService {
private _config: BehaviorSubject<object> = new BehaviorSubject(null);
public config: Observable<object> = this._config.asObservable();
constructor(private api: APIService) {
this.loadConfigs();
}
loadConfigs() {
this.api.get('/configs').subscribe( res => this._config.next(res) );
}
}
正在尝试从组件中调用它:
...
Observable.forkJoin([someService.config])
.subscribe( res => console.log(res) ) //not working
someService.config.subscribe( res => console.log(res) ) // working
...
如何将 Observable.forkJoin
与 Observable
变量 config
一起使用?
我需要将配置存储在服务中并等待它们熄灭,其他人请求未完成停止加载程序。
由于您使用的是 BehaviorSubject
,因此您应该知道可以手动调用 next()
和 complete()
。
forkJoin()
运算符仅在其所有源 Observable 都已发出至少一个值时才会发出 并且 它们都已完成。由于您使用的是 Subject 和 asObservable
方法,源 Observable 永远不会完成,因此 forkJoin
运算符永远不会发出任何东西。
顺便说一句,仅对一个源 Observable 使用 forkJoin
没有多大意义。也可以查看类似的 zip()
或 combineLatest()
运算符,也许这就是您所需要的。
两个非常相似的问题:
- ForkJoin 2 BehaviorSubjects
我有服务:
export class ConfigService {
private _config: BehaviorSubject<object> = new BehaviorSubject(null);
public config: Observable<object> = this._config.asObservable();
constructor(private api: APIService) {
this.loadConfigs();
}
loadConfigs() {
this.api.get('/configs').subscribe( res => this._config.next(res) );
}
}
正在尝试从组件中调用它:
...
Observable.forkJoin([someService.config])
.subscribe( res => console.log(res) ) //not working
someService.config.subscribe( res => console.log(res) ) // working
...
如何将 Observable.forkJoin
与 Observable
变量 config
一起使用?
我需要将配置存储在服务中并等待它们熄灭,其他人请求未完成停止加载程序。
由于您使用的是 BehaviorSubject
,因此您应该知道可以手动调用 next()
和 complete()
。
forkJoin()
运算符仅在其所有源 Observable 都已发出至少一个值时才会发出 并且 它们都已完成。由于您使用的是 Subject 和 asObservable
方法,源 Observable 永远不会完成,因此 forkJoin
运算符永远不会发出任何东西。
顺便说一句,仅对一个源 Observable 使用 forkJoin
没有多大意义。也可以查看类似的 zip()
或 combineLatest()
运算符,也许这就是您所需要的。
两个非常相似的问题:
- ForkJoin 2 BehaviorSubjects