【发布时间】:2019-08-16 08:42:28
【问题描述】:
为什么这个功能只能工作一次?我单击一个按钮来调用主题队列上的 next() 有效,但如果我单击另一个按钮它不起作用。
getData(text): Observable<string> {
const timer$ = timer(2000);
const observable = new Observable<string>(observer => {
timer$.pipe(
map(() => {
observer.next('http response ' + text);
})
).subscribe();
});
return observable;
}
我设置了一个 Subject 并使用 next() 应该使 observable 发出数据。
queue = new Subject();
streamA$: Observable<string>;
streamB$: Observable<string>;
images$: Observable<string>;
constructor(private timerService: TimerService) {
}
ngOnInit() {
this.streamA$ = this.timerService.getData('a');
this.streamB$ = this.timerService.getData('b');
this.images$ = this.queue.pipe(concatMap((data: string) => data));
}
clickA() {
this.queue.next(this.streamA$);
}
clickB() {
this.queue.next(this.streamB$);
}
模板:
<button (click)="clickA()">Click A</button>
<button (click)="clickB()">Click B</button>
<div>{{images$ | async}}</div>
【问题讨论】:
-
你正在推送一个字符串的可观察对象,这没有任何意义。
-
刚刚学习 rxjs observable 以便测试操作符
标签: rxjs