"" /> " - 且购网" />

且构网

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

NgRX 效果 - 类型“可观察<未知"不可分配到类型“Observable"

更新时间:2022-11-30 12:24:05

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

替代方案 - 像下面这样重写也有效

Alternative - rewriting like the following also works

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

附加

执行上述操作后仍然出错?类型检查正在正确地完成它的工作,并告诉您应该映射到 Observable 或添加第二个参数 { dispatch: false }(即不分派动作).请参阅NgRx 效果文档

Still 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.