首页 > 解决方案 > 如果动作是从另一个模块分派的,ngrx 效果不会运行

问题描述

所以这就是我想要做的。

在延迟加载BookmarksModule功能模块下,我有一个效果,即监听authActions.loginSuccessw/c 在 w/c 中注册AuthModule不是延迟加载的。

@Injectable({
  providedIn: 'root',
})
export class FavoriteEffects {
  getFavoriteAfterLogin$ = createEffect(
    () => {
      return this.actions$.pipe(
        ofType(authActions.loginSuccess),
        map(() => favoriteActions.getFavorites())
      );
    }
  );

authActions.loginSuccess是发送的方式。

loginCallback$ = createEffect(() => {
  return this.actions$.pipe(
    ofType(authActions.loginCallback),
    exhaustMap(() =>
      this.authService.currentUser$.pipe(
        map(currentUser => authActions.loginSuccess({ currentUser })),
        catchError(error =>
          of(authActions.loginFailure({ error: error?.error?.message }))
        )
      )
    )
  );
});

loginSuccessRedirect$ = createEffect(
  () => {
    return this.actions$.pipe(
      ofType(authActions.loginSuccess),
      tap(() => this.router.navigate([this.redirects.login.success]))
    );
  },
  { dispatch: false }
);

正如你所看到的,当authActions.loginSuccess被调度时,它loginSuccessRedirect$在同一个类中被正确拦截,但是这个动作没有在延迟加载的模块中被拦截?

这是ngrx store devtools中的动作截图 在此处输入图像描述

似乎update-reducers功能模块的动作是在动作已经被调度之后调度的。loginSuccess

我该如何解决这个问题?

标签: angularngrxngrx-storengrx-effectsngrx-entity

解决方案


推荐阅读