参数“action”和“action”的类型不兼容,Angular ngrx 中缺少属性“payload”

2023-12-23

我是角度新手。在这里,我使用 ngrx 来管理我的 Angular 应用程序中的状态。但是当我编译时出现以下错误。它说“参数‘action’和‘action’的类型不兼容”。我想知道这个问题的原因以及如何解决?

 
    Error: src/app/shopping-list/store/shoppingList.actions.ts:9:5 - error TS2564: Property 'payload' has no initializer and is not definitely assigned in the constructor.
    
    9     payload: Ingredient;
          ~~~~~~~
    src/app/app.module.ts:25:27 - error TS2322: Type '(state: { ingredients: Ingredient[]; } | undefined, action: AddIngredient) => { ingredients: Ingredient[]; }' is not assignable to type 'ActionReducer<{ ingredients: Ingredient[]; }, Action>'.
      Types of parameters 'action' and 'action' are incompatible.
        Property 'payload' is missing in type 'Action' but required in type 'AddIngredient'.
    
    25     StoreModule.forRoot({ shoppingList: shoppingListReducer }),
                                 ~~~~~~~~~~~~
    
      src/app/shopping-list/store/shoppingList.actions.ts:9:5
        9     payload: Ingredient;
              ~~~~~~~
        'payload' is declared here.

这是我的 shoppingList.actions.ts 文件。

import { Action } from '@ngrx/store'

import { Ingredient } from '../../shared/ingredient.model';

export const ADD_INGREDIENT = 'ADD_INGREDIENT';

export class AddIngredient implements Action {
    readonly type = ADD_INGREDIENT;
    payload: Ingredient;
}

这是 shoppingList.reducer.ts 文件。


import { Ingredient } from "src/app/shared/ingredient.model";

import * as shoppingListActions from './shoppingList.actions';

const intialState = {
    ingredients: [
        new Ingredient("Apples", 3),
        new Ingredient("Tomatoes", 4)
    ]
}

export function shoppingListReducer(state = intialState, action: shoppingListActions.AddIngredient) {
    switch (action.type) {
        case shoppingListActions.ADD_INGREDIENT:
            return {
                ...state,
                ingredients: [...state.ingredients, action.payload]
            }
        default:
            return state;
    }
}

这是我的 app.module.ts 文件。

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRouting } from './app-routing.module';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { StoreModule } from '@ngrx/store';

import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { SharedModule } from './shared/shared.module';
import { CoreModule } from './core.module';
import { shoppingListReducer } from './shopping-list/store/shoppingLis.reducer';

@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,

  ],
  imports: [
    BrowserModule,
    FormsModule,
    ReactiveFormsModule,
    HttpClientModule,
    StoreModule.forRoot({ shoppingList: shoppingListReducer }),
    AppRouting,
    SharedModule,
    CoreModule,
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }


您的问题是由于签名问题,

方法 StoreModule.forRoot(reducers: ActionReducerMap)

从技术上讲,您传递了正确的参数,但仍然表明您的Action对象参数包含字段调用payload不存在于Action的参数ActionReducerMap。从技术上讲,这不应该是一个错误,因为您已经继承了Action到你的动作课

类 AddIngredient 实现 Action{

但不幸的是,很明显,行动ActionReducerMap不是 从'@ngrx/商店'或者它们不相同,因此会出现编译错误。

由于您没有任何其他选择,因此您必须按如下方式修复它:-

首先创建你的State就像下面你的购物清单.reducer.ts

export interface ShoppingListState{
    ingredients: Ingredient[];
}

const initialState: ShoppingListState = {
    ingredients: [
        new Ingredient('Apples', 5),
        new Ingredient("Tomatoes", 4),
    ]
};

还要修改您的减速器方法,如下所示:-

export function shoppingListReducer(state: ShoppingListState = initialState, 
    action: shoppingListActions.AddIngredient): ShoppingListState {
    switch(action.type){

现在创建一个文件调用index.ts(在减速器文件的同一位置 - 您也可以提供不同的名称)如下所示在您的操作文件夹下

import { ShoppingListState,  shoppingListReducer } from './shopping-list.reducer';
import { ActionReducerMap } from '@ngrx/store';


export const rootReducer = {};

export interface AppState {
    shoppingList: ShoppingListState;
};


export const reducers: ActionReducerMap<AppState, any> = {
    shoppingList: shoppingListReducer
};

现在将此减速器导入到您的应用程序模块.ts

import { reducers } from './reducers/'

并修改你的代码如下

StoreModule.forRoot(reducers)

还要尽量避免像下面这样声明变量。

payload: Ingredient;

更好地使用构造函数并修改您的代码,如下所示:-

export class AddIngredient implements Action {
    readonly type = ADD_INGREDIENT;
    constructor(public payload: Ingredient){}
}

希望这能解决您的问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

参数“action”和“action”的类型不兼容,Angular ngrx 中缺少属性“payload” 的相关文章