NgRX effects - Type 'Observable' is not assignable to type 'Observable'

前端 未结 9 1104
你的背包
你的背包 2020-12-16 10:12

While working with NgRX 8 my colleagues and me are frequently facing a weird error message when implementing the effects.

Type \'Observable

9条回答
  •  鱼传尺愫
    2020-12-16 10:19

    Quick version
    comment out createEffect(() =>,
    fix errors that your IDE (VSCode) flags up,
    add createEffect(() => back in.

    Alternative - rewriting like the following also works

    someEffect$ = createEffect(() => {
      return this.actions$.pipe(
        ...
      )
    })
    

    Additional

    No errors after doing the above? Type-checking is doing it's job correctly and telling you that you should be mapping to an Observable or for a purely side-effect effect adding the second argument { dispatch: false } (i.e. not dispatching an action). See the NgRx Effects Docs


    Older Answer (using @Effect is unneccessary and is not required)

    The easiest way I've found to debug is to write in a version 7 manner with the @Effect decorator and once done rewrite using createEffect.

    So to debug:

      navigateToDashboard$ = createEffect(() =>
        this.actions$.pipe(
          ofType(teamActions.CREATE_SUPERVISOR_GROUP_SUCCESS),
          map((action: teamActions.CreateSupervisorGroupSuccess) => action.payload),
          map((team: Team) => team.TeamID),
          SwitchMap(id => new routerActions.Go({ path: ['/team', id, 'populate'] }))
        )
      )
    

    which gives the non-helpful error write as (add decorator, delete createEffect(() =>, delete final bracket),

    @Effect()
    navigateToDashboard$ = this.actions$.pipe(
        ofType(teamActions.CREATE_SUPERVISOR_GROUP_SUCCESS),
        map((action: teamActions.CreateSupervisorGroupSuccess) => action.payload),
        map((team: Team) => team.TeamID),
        SwitchMap(id => new routerActions.Go({ path: ['/team', id, 'populate'] }))
    )
    

    Now we get error

    Cannot find name 'SwitchMap'
    

    Followed by

    Type 'Go' is not assignable to type 'ObservableInput'
    

    Fixing this gives

    @Effect()
    navigateToDashboard$ = this.actions$.pipe(
        ofType(teamActions.CREATE_SUPERVISOR_GROUP_SUCCESS),
        map((action: teamActions.CreateSupervisorGroupSuccess) => action.payload),
        map((team: Team) => team.TeamID),
        switchMap(id => of(new routerActions.Go({ path: ['/team', id, 'populate'] })))
    )
    

    Now rewrite in NgRx 8 terms. Not pretty but works.

提交回复
热议问题