且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

NgRX 8效果-类型“可观察到的"不可分配给"Observable< Action>"类型

更新时间:2022-11-30 11:57:21

快速版本
注释掉createEffect(() =>
修复您的IDE(VSCode)标记的错误,
重新添加createEffect(() =>.

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(
    ...
  )
})

其他

完成上述操作后没有错误? 类型检查可以正确完成它的工作,并告诉您应该映射到Observable<Action>或添加第二个参数{ dispatch: false }以获得纯粹的副作用(即不分派动作).请参见 NgRx效果文档

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<Action> or for a purely side-effect effect adding the second argument { dispatch: false } (i.e. not dispatching an action). See the NgRx Effects Docs

较旧的答案(不必,也不需要使用@Effect)

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

我发现调试的最简单方法是使用@Effect装饰器以第7版的方式编写,然后使用createEffect重写.

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.

要调试:

  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'] }))
    )
  )

将无用错误写为(添加装饰器,删除createEffect(() =>,删除最后一个括号)

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'] }))
)

现在我们得到了错误

Cannot find name 'SwitchMap'

之后

Type 'Go' is not assignable to type 'ObservableInput<any>'

修复此问题

@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'] })))
)

现在用NgRx 8项重写.不漂亮,但是可以.

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