Fus*_*sin 0 http rxjs ngrx ngrx-effects angular
我有这种效果,我正在尝试使用计时器每x秒轮询一次数据。但是我不知道计时器应该如何与数据流交互。我尝试在顶部添加另一个switchMap,但随后无法将操作和有效负载传递给第二个switchmap。有任何想法吗?
我看了这篇文章,但情况有所不同。我正在通过需要访问的操作传递有效负载,并且正在使用ngrx 6。
@Effect()
getData = this.actions$
.ofType(appActions.GET_PLOT_DATA)
.pipe(
switchMap((action$: appActions.GetPlotDataAction) => {
return this.http.post(
`http://${this.url}/data`,
action$.payload.getJson(),
{responseType: 'json', observe: 'body', headers: this.headers});
}),
map((plotData) => {
return {
type: appActions.LOAD_DATA_SUCCESS,
payload: plotData
}
}),
catchError((error) => throwError(error))
)
Run Code Online (Sandbox Code Playgroud)
这应该工作(我已经测试过)。请在的顶部添加switchMap。这里的主要操作员是mapTo。该运算符会将间隔的输入值映射到有效负载中。
switchMap((action$: appActions.GetPlotDataAction) =>
interval(5000).pipe(mapTo(action$))
);
Run Code Online (Sandbox Code Playgroud)
更新(提示):
-如果要立即开始轮询,然后每{n} ms可以使用startWith运算符或timer可观察值
switchMap((action$: appActions.GetPlotDataAction) =>
interval(5000).pipe(startWith(0), mapTo(action$))
);
Run Code Online (Sandbox Code Playgroud)
要么
switchMap((action$: appActions.GetPlotDataAction) =>
timer(0, 1000).pipe(mapTo(action$))
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1038 次 |
| 最近记录: |