NgRX效果-类型“可观察的"不可分配给"Observable< Action>"类型 [英] NgRX effects - Type 'Observable<unknown>' is not assignable to type 'Observable<Action>'

查看:66
本文介绍了NgRX效果-类型“可观察的"不可分配给"Observable< Action>"类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用NgRX 8时,我和我的同事在实现效果时经常会遇到奇怪的错误消息.

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

类型'Observable< unknown>'不可分配给类型'Observable< Action> |((... args:any [])=> Observable< Action>)'

Type 'Observable<unknown>' is not assignable to type 'Observable<Action> | ((...args: any[]) => Observable<Action>)'

它与类型问题有关.消息如此具体,真是令人讨厌,它标志着完整的效果.这种情况经常出现,而且确实很难解决.

It is related to type issues. It is really annoying that the message is so unspecific and it marks the complete effect. This appears frequently and is really hard to resolve.

我们想知道是否可以做一些事情来快速发现问题,或者我们是否能够以某种方式破坏消息的结构.我不是在这里寻找特定的解决方案,而是在寻找一种如何快速确定出问题的方法"的程序.

We are wondering if there is something we can do in order to quickly identify the problems or if we are able to destructure the message in some way. I am not looking for a specific solution here, but more for a "how to quickly determine what's wrong"-procedure.

谢谢和欢呼!

这是我们到目前为止所做的,也是来自评论和答案的想法.

It is what we have done so far and also ideas from comments and answers.

  • 在大多数情况下,动作可能不是问题
  • switchMap 参数
  • 可能是错误的解构
  • 其中一个RxJS运算符未导入
  • 错误的参数和服务中的返回值可能是原因
  • 服务和操作类型不一致也是原因
  • 如果没有任何帮助,并且您确定没有问题,请执行以下操作:重新加载TypeScript

我们仍然希望有一个技巧来分解错误消息.

We are still hoping for a trick to break down the error message.

推荐答案

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

替代-类似于以下内容的重写

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.

这篇关于NgRX效果-类型“可观察的"不可分配给"Observable&lt; Action&gt;"类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆