[英]How to get data from the server through an interval?
I use angular6, i try get json from server through 5 seconds interval.我使用 angular6,我尝试通过 5 秒的间隔从服务器获取 json。
Its my service:它是我的服务:
getExample(): Observable<any> {
console.log('getExample');
return Observable
.timer(0, 5000)
.flatMap(() => this.httpClient.get('https://jsonplaceholder.typicode.com/posts'))
.map((r) => {
return r;
});
}
Please help fix this code.请帮助修复此代码。
PS: more simple service worked without problem PS:更多简单的服务工作没有问题
RxJS 6 has updated to use pipeable operators . RxJS 6 已更新为使用可管道操作符。 Also, timer imports from a different path .
此外,计时器从不同的路径导入。 You should import
timer
(or interval
in this case) directly and use .pipe
with the operators that you need.您应该直接导入
timer
(或在这种情况下的interval
)并使用.pipe
和您需要的运算符。 Finally, use mergeMap
instead of flatMap
.最后,使用
mergeMap
而不是flatMap
。
import { interval } from 'rxjs';
import { mergeMap, map } from 'rxjs/operators';
return interval(5000).pipe(
mergeMap(() => this.httpClient.get('https://jsonplaceholder.typicode.com/posts')),
map((r) => {
console.log(r);
return r;
})
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.