ngrxOnRunEffects方法停止效果调度其他效果

问题描述

我做什么

我有一个Angular应用程序,我正在实现NGRX 10,以便在登录后管理帐户信息。 因此,我做了登录操作,当用户登录或加载应用程序并从服务器加载会话时调用

这就是我实现登录效果的方式

:key="alert.val"

当我访问先前打开的会话的页面受限部分时,我需要确保已从服务器加载帐户信息。

然后,我尝试在我的应用模块中实现类似以下代码内容

  LogIn$ = createEffect(
    () => this.actions$.pipe(
      ofType(UserActions.logInUser),switchMap(( action ) => {
        console.log('login');
        return this.backendService.login(action.credentials,action.remember).pipe(
          mergeMap(data => [
            UserActions.logInUserSuccess(data),UserActions.saveOAuthInCookie(data)
          ]),catchError( error => of(UserActions.loginUserFail(error)))
        );
      })
    ),{ dispatch: true }
  );

,并且在我的效果类中实现export function initApplication(store: Store<IState>): Function { return () => new Promise((resolve,reject) => { store.dispatch(StartAppInitializer({})); store.dispatch(loadOAuthFromCookie({})); store.select((state) => state.user) .pipe( filter(user => ((user.oAuthToken && user.oAuthToken.length > 0 && (user.account && user.account.email && user.account.email.length > 0) ) || (!user.oAuthToken || user.oAuthToken.length === 0))),take(1) ).subscribe((account) => { store.dispatch(FinishAppInitializer({})); resolve(false); }); }); } 和以下代码

OnRunEffects

这将更改为等待应用程序调用{​​{1}}。

我想要什么

我要停止加载应用程序,直到NGRX从服务器获取帐户信息为止

我得到的东西

在我对效果类实现ngrxOnRunEffects之前,我就有效果了,但是在我对效果类实现ngrxOnRunEffects之后,其他效果停止了调度其他动作。

这是一些图片,用于说明我登录后发生的情况:

Image Before I implements ngrxOnRunEffects

Image After I implements ngrxOnRunEffects

我找到的解决方

首先,我对此进行了更改:

  ngrxOnRunEffects(resolvedEffects$: Observable<EffectNotification>): Observable<EffectNotification> {
      return this.actions$.pipe(
        ofType('LOGGED_IN'),exhaustMap(() =>
          resolvedEffects$.pipe(
            takeuntil(this.actions$.pipe(ofType('LOGGED_OUT')))
          )
        )
      );
    }

为此:

FinishAppInitializer

此更改导致应用程序停止,直到兑现承诺为止。

然后,我将ngrxOnRunEffects方法移至另一个类,这解决了没有其他效果调用的问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)