代码之家  ›  专栏  ›  技术社区  ›  Alex Verzea

ngrx效果不触发动作

  •  2
  • Alex Verzea  · 技术社区  · 8 年前

    我对ngrx有问题。在效果中,我在NodeJS中调用后端路由,如下所示:

    @Effect() 
    authSignup = this.actions$ 
        .ofType(AuthActions.TRY_SIGNUP)
        .switchMap((action: AuthActions.TrySignup) => { 
            return this.httpClient.post('nodejs_backend_route') 
            .map((response: any) => { 
                return Observable.of({ type: AuthActions.SUCCESSFUL_SIGNUP }); 
            })
        });
    

    如果路线在里面 .post 是NodeJS中的后端路由,该操作在内部激发 .map 无法识别。我收到以下错误:

    “Error: Effect "AuthEffects.authSignup" dispatched an invalid action: [object Object]”

    然而行动 AuthActions.SUCCESSFUL_SIGNUP 存在。如果没有调用后端,则操作将完全启动。从ngrx效果内部调用NodeJS后端是否存在问题?

    谢谢

    1 回复  |  直到 8 年前
        1
  •  4
  •   Kamil    7 年前

    可以这样尝试:

    @Effect()
    authSignup$: Observable<AuthActions.All> = this.actions$
      .pipe(
        ofType<AuthActions.TrySignup>(AuthActions.TRY_SIGNUP),
        switchMap((action) => this.httpClient.post('nodejs_backend_route')
          .pipe(
            map((response) => new AuthActions.SuccessfulSignup(response)),
            catchError((error) => of(new AuthActions.FailedSignup(error)))
          )
        )
      );
    

    问题是您没有返回 Action 在您的 map 。我重写了它,给你一个提示,如何使用最新版本的供应商来完成它。

    推荐文章