NgRx - 可选 return 来自效果的一个或两个动作
NgRx - optionally return either one OR two actions from an effect
我有一个效果,根据数据返回的内容,我可能希望提交额外的操作。
使用 this post 中的信息,我可以 return 通过以下两个操作...
public getData$ = createEffect(() => this.actions$.pipe(
ofType(myDataActions.getData),
map(() => this.isPollingActive = true),
mergeMap(() =>
this.myService.getAllData()
.pipe(
tap(data => this.previousResultsTimeUtc = data.previousResultsTimeUtc),
mergeMap(data => [
currentDayActions.getCurrentShiftSuccess(data.currentDay),
myDataActions.getDataSuccess(data)
]),
catchError(err => of(myDataActions.getDataFail(err)))
))
));
然而,理想情况下,我有时只想提交一个动作,
例如
...
mergeMap(data => [
if (data.currentDay !== undefined) // <-- how to do this
currentDayActions.getCurrentDaySuccess(data.currentDay),
myDataActions.getDataSuccess(data.data)
]),
所以,如果我得到数据,我只想提交currentDayActions.getCurrentDaySuccess
。
当然以上是不正确的语法,但我不太明白如何在此处获取此 "if"。
更新
非常相似的例子是here
试图做同样事情的效果在feature1/state/feature1.effects.ts
一个if else
语句就可以了:
public continuePolling$ = createEffect(() =>
this.actions$.pipe(
ofType(
feature1Actions.startPollSuccess,
takeWhile(() => this.isPollingActive),
mergeMap(() =>
this.feature1Service.getData().pipe(
delay(8000),
tap(
data =>
(this.previousResultsTimeUtc = data.previousResultsTimeUtc)
),
switchMap(data => {
if (data.currentDay == undefined) {
return [feature1Actions.getLibrarySuccess(data)];
} else {
return [
feature1Actions.getCurrentDaySuccess(data.currentDay),
feature1Actions.getLibrarySuccess(data)
];
}
}),
catchError(err => of(feature1Actions.getLibraryFail(err)))
)
)
)
)
);
我有一个效果,根据数据返回的内容,我可能希望提交额外的操作。
使用 this post 中的信息,我可以 return 通过以下两个操作...
public getData$ = createEffect(() => this.actions$.pipe(
ofType(myDataActions.getData),
map(() => this.isPollingActive = true),
mergeMap(() =>
this.myService.getAllData()
.pipe(
tap(data => this.previousResultsTimeUtc = data.previousResultsTimeUtc),
mergeMap(data => [
currentDayActions.getCurrentShiftSuccess(data.currentDay),
myDataActions.getDataSuccess(data)
]),
catchError(err => of(myDataActions.getDataFail(err)))
))
));
然而,理想情况下,我有时只想提交一个动作,
例如
...
mergeMap(data => [
if (data.currentDay !== undefined) // <-- how to do this
currentDayActions.getCurrentDaySuccess(data.currentDay),
myDataActions.getDataSuccess(data.data)
]),
所以,如果我得到数据,我只想提交currentDayActions.getCurrentDaySuccess
。
当然以上是不正确的语法,但我不太明白如何在此处获取此 "if"。
更新
非常相似的例子是here
试图做同样事情的效果在feature1/state/feature1.effects.ts
一个if else
语句就可以了:
public continuePolling$ = createEffect(() =>
this.actions$.pipe(
ofType(
feature1Actions.startPollSuccess,
takeWhile(() => this.isPollingActive),
mergeMap(() =>
this.feature1Service.getData().pipe(
delay(8000),
tap(
data =>
(this.previousResultsTimeUtc = data.previousResultsTimeUtc)
),
switchMap(data => {
if (data.currentDay == undefined) {
return [feature1Actions.getLibrarySuccess(data)];
} else {
return [
feature1Actions.getCurrentDaySuccess(data.currentDay),
feature1Actions.getLibrarySuccess(data)
];
}
}),
catchError(err => of(feature1Actions.getLibraryFail(err)))
)
)
)
)
);