简化版
将createEffect(() =>
注释掉,
修复你的IDE(VSCode)标记出的错误,
再将createEffect(() =>
添加回去。
另一种方式 - 以下重写也可以工作
someEffect$ = createEffect(() => {
return this.actions$.pipe(
...
)
})
另外提示
以上操作后仍然存在错误吗?
类型检查正在正确地执行它的工作,并告诉您应该映射到 Observable<Action>
或者作为纯副作用效果添加第二个参数 {dispatch: false}
(即不分派动作)。请参阅 NgRx Effects 文档
旧回答(使用 @Effect
是不必要的,也不是必需的)
我发现调试最简单的方法是以版本7方式编写代码,使用@Effect
装饰器,完成后再使用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(() =>
,最后删除最后一个括号。
@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的术语进行重写。不太美观但有效。
addComment
? - Yanis-gitaddComment
是createAction
方法的返回值,可能不是问题所在。 而failed
和success
方法是由createAction
生成的操作。 在这里,failed
不是问题,尽管我没有将其作为函数执行。 - Felix LemkecreateAction
创建的。 - KiraAG