瀟湘沐
2022-07-15 10:12:47
下面是一個將加載操作分派到商店的操作。相應的效果將處理請求并發送回響應項。但我想要的是我想用一個按鈕來切換下面的動作。因此,如果我按開始,它將每 1 秒開始調度一次動作,如果我按暫停,它將暫停調度,如果我按開始,它將從它離開的地方繼續,并重復......我怎樣才能切換這樣的動作? let date = 1587513626000; // date is required because the backend only sends data with a start and end date interval(1000).pipe(tap(_ => { this.store.dispatch(loadStoreItems({ limit: 10, start: date, end: date + 1000 })) date += 1000 })) .subscribe()我嘗試了一堆運算符,其中一些正在部分工作(例如有時使用 takeWhile/takeUntil 我能夠暫停)但無法重新啟動。
1 回答

慕田峪7331174
TA貢獻1828條經驗 獲得超13個贊
要在效果中進行切換,您需要 2 個操作(開始和停止)并使用takeUntil.
//effects.ts
loadCourierItems$ = createEffect(() =>
this.actions$.pipe(
ofType(actions.start),
exhaustMap(action => interval(1000).pipe(
// logic for every second activity.
map(actions.everySecondAction()),
)),
takeUntil(this.actions$.pipe(ofType(actions.stop))),
repeat(),
)
)
//app.component.ts
constructor(private store: Store<CourierItemsState>) {
}
ngOnInit() {
this.store.dispatch(startAction());
}
ngOnDestroy() {
this.store.dispatch(stopAction());
}
添加回答
舉報
0/150
提交
取消