参数“action”和“action”的类型在 Angular ngrx 商店项目中不兼容

Arc*_*yal 3 web typescript ngrx angular ngrx-store

我正在使用 Angular 创建我的第一个 NGRX 项目,并遵循本教程(请在此处找到所有文件代码)。

当我在 app.mdule.ts 中声明减速器时,我遇到了这个编译错误。

Type '(state: Tutorial[] | undefined, action: Actions) => Tutorial[]' is not assignable to type 'ActionReducer<Tutorial[], Action>'.
  Types of parameters 'action' and 'action' are incompatible.
    Type 'Action' is not assignable to type 'Actions'.
      Property 'payload' is missing in type 'Action' but required in type 'RemoveTutorial'.
22       **tutorial: reducer**
         ~~~~~~~~
  src/app/actions/tutorial.actions.ts:20:17
    20     constructor(public payload: Tutorial) { }
                       ~~~~~~~~~~~~~~~~~~~~~~~~
    'payload' is declared here.
Run Code Online (Sandbox Code Playgroud)
 imports: [
    BrowserModule,
    AppRoutingModule,
    StoreModule.forRoot({
      tutorial: reducer ///this line is throwing above error
    })
  ],
Run Code Online (Sandbox Code Playgroud)

Dav*_*ebj 5

如果你想解决这个问题,只需在 ngrx 的通用 Action 中更改函数中的 TutorialAction 即可。然后在函数内声明一个常量作为TutorialAction,如下所示:

export function reducer(state: Tutorial[] = [initialState], action: Action) {
    const tutorialAction = action as TutorialActions.Actions; 

     switch(tutorialAction .type) { .. }

}
Run Code Online (Sandbox Code Playgroud)

让我知道它是否有效