Pra*_*M96 4 typescript ngrx angular ngrx-store angular-ngrx-data
I am new to angular. Here I using ngrx to manage a state in my angular app. But when I'm compiling I got the following error. It says that 'Types of parameters 'action' and 'action' are incompatible'. I want to know the reason for this and how to solve this?
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.
Run Code Online (Sandbox Code Playgroud)
This is my shoppingList.actions.ts file.
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;
}
Run Code Online (Sandbox Code Playgroud)
This is the shoppingList.reducer.ts file.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
Here is my app.module.ts file.
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 { }
Run Code Online (Sandbox Code Playgroud)
你的问题是因为签名问题,
方法 StoreModule.forRoot<unknown, Action>(reducers: ActionReducerMap<unknown, Action>)
从技术上讲,您正在传递一个正确的参数,但它仍然说您的Action对象参数包含一个字段调用有效负载,它不存在于ActionReducerMap<unknown, Action> 的Action参数中。从技术上讲,它不应该是错误,因为您已经将Action继承到您的操作类
类 AddIngredient 实现 Action{
但不幸的是,很明显ActionReducerMap<unknown, Action>中的Action不是来自'@ngrx/store'或者它们不相同,因此给出了编译错误。
由于您没有任何其他选择,您必须像下面那样修复它:-
首先创建国象下面这样在你购物,list.reducer.ts
export interface ShoppingListState{
ingredients: Ingredient[];
}
const initialState: ShoppingListState = {
ingredients: [
new Ingredient('Apples', 5),
new Ingredient("Tomatoes", 4),
]
};
Run Code Online (Sandbox Code Playgroud)
还要修改您的减速器方法,如下所示:-
export function shoppingListReducer(state: ShoppingListState = initialState,
action: shoppingListActions.AddIngredient): ShoppingListState {
switch(action.type){
Run Code Online (Sandbox Code Playgroud)
现在在您的操作文件夹下创建一个文件调用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
};
Run Code Online (Sandbox Code Playgroud)
现在将此减速器导入您的app.module.ts
import { reducers } from './reducers/'
Run Code Online (Sandbox Code Playgroud)
并修改您的代码,如下所示
StoreModule.forRoot(reducers)
Run Code Online (Sandbox Code Playgroud)
还要尽量避免像下面这样声明变量。
payload: Ingredient;
Run Code Online (Sandbox Code Playgroud)
更好地使用构造函数并修改您的代码,如下所示:-
export class AddIngredient implements Action {
readonly type = ADD_INGREDIENT;
constructor(public payload: Ingredient){}
}
Run Code Online (Sandbox Code Playgroud)
希望这会解决您的问题。
| 归档时间: |
|
| 查看次数: |
1919 次 |
| 最近记录: |