我在 Angular 中使用 ngrx。我想这样做,以便在加载图像数组后执行重定向。但我不进行重定向并收到此错误
Error: Effect "CreateRequestEffects.uploadFile$" dispatched an invalid action: {}
uploadFile$ = createEffect(() => {
return this.actions$.pipe(
ofType(uploadFile),
switchMap((action) => {
return this.createRequestService.uploadFile(action.files, action.post_id).pipe(
takeUntil(
this.actions$.pipe(
ofType(UPLOAD_CANCEL)
),
),
map((data) => {
this.store$.dispatch(uploadFileSuccess({ image: data }))
return this.router.navigate([`/requests/post/${data[data.length].post_id}`]);
}),
catchError((err) => {
return of(err);
})
);
})
)
});
{ dispatch: false }
,NgRx 效果应始终返回操作。这是你的情况。我相信这个小小的改变应该足够了:
map((data) => {
// Start navigation first
this.router.navigate([`/requests/post/${data[data.length].post_id}`]);
// Then return the success action
return uploadFileSuccess({ image: data });
}),