尝试从服务器获取数据,每 10 秒间隔一次服务。如果价格返回,则停止区间。代码如下:
但我收到错误:
错误 TypeError:您在需要流的地方提供了“未定义”。您可以提供 Observable、Promise、Array 或 Iterable。
代码:
public Respond() {
this.dataService.WaitingForServiceRespond()
.pipe((
debounceTime(2000),
switchMap((r: any) => {
if (!r.price) {
setInterval(() => {
return this.Respond();
}, 10000)
} else {
this.dataService.user.payment = r;
console.log('price returned', r);
return ''
}
})
))
.subscribe(e => {
console.log(e)
})
Run Code Online (Sandbox Code Playgroud)
}
问题出在你的switchMap. 它期望返回一个流。使用时不会返回任何内容setInterval。您可以Observable通过返回interval()ofrxjs而不是调用来返回 an setInterval()。
import { interval } from 'rxjs';
...
public Respond() {
this.dataService.WaitingForServiceRespond()
.pipe((
debounceTime(2000),
switchMap((r: any) => {
if (!r.price) {
return interval(10000).pipe(tap(() => this.Respond()))
} else {
this.dataService.user.payment = r;
console.log('price returned', r);
return of('')
}
})
))
.subscribe(e => {
console.log(e)
})
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3669 次 |
| 最近记录: |