【问题标题】:Property does not exist on type 'never' in Ngrx effectNgrx 效果中的“从不”类型上不存在属性
【发布时间】:2019-07-10 07:58:57
【问题描述】:

我正在使用 Angular 8 和 NGRX 8。我有一个动作:

export const loadEnvironment = createAction(
  LicencingActionTypes.LoadEnvironment,
  props<{environment: Environment}>()
);

及对应效果:

 loadEnvironment = createEffect(() => this.actions$.pipe(
    ofType(LicencingActionTypes.LoadEnvironment),
    exhaustMap((action) =>
      this.authService.
      getToken(LicencingEffects.getAuthDetailsForEnvironment(action.environment))
        .pipe(
          switchMap((token) => [
            AuthActions.onLogin({token: token}),
            LicencingActions.onLoadEnvironment({environment: action.environment})
          ]),
          catchError(error => of(AuthActions.onLoginError({error: error})))
        )
    )
  ));

我一直在阅读有关 NGRX 8 (https://ngrx.io/guide/effects#incorporating-state) 的文档。

他们的例子表明你可以只使用 action 属性而不需要转换动作的类型:

...
exhaustMap(action =>
        this.authService.login(action.credentials)
...

Webpack 无法编译,我收到以下错误:

ERROR in src/app/licencing/effects/licencing.effects.ts(20,69): error TS2339: Property 'environment' does not exist on type 'never'.

Screenshot of code with errors highlighted

我哪里错了?

【问题讨论】:

    标签: ngrx-effects angular8


    【解决方案1】:

    动作必须在动作文件底部的类型联合中声明:

    const all = union({
      info,
      appError
    });
    
    export type CoreActionsUnion = typeof all;
    

    然后在 Effects 类的构造函数中注入该类型作为 Actions 的类型参数:

    constructor(private actions$: Actions<CoreActionsUnion>) { }
    

    【讨论】:

      【解决方案2】:

      尝试如下更改您的effect -

      loadEnvironment = createEffect(() => this.actions$.pipe(
          ofType(loadEnvironment), //<-- this is your action
          exhaustMap(action => //<-- getting rid of extra parenthesis
            this.authService.
            getToken(LicencingEffects.getAuthDetailsForEnvironment(action.environment))
              .pipe(
                switchMap((token) => [
                  AuthActions.onLogin({token: token}),
                  LicencingActions.onLoadEnvironment({environment: action.environment})
                ]),
                catchError(error => of(AuthActions.onLoginError({error: error})))
              )
          )
        ));
      

      示例参考implementation

      【讨论】:

        猜你喜欢
        • 2018-08-06
        • 2020-06-24
        • 2018-04-25
        • 2018-01-06
        • 1970-01-01
        • 2017-10-24
        • 2021-12-30
        • 2017-04-09
        • 2018-03-09
        相关资源
        最近更新 更多