该问题通常是因为没有使用正确的reducer函数处理actions和state,或没有正确初始化state。以下是一些可能的解决方案:
确保reducer函数能够正确处理actions和state。在reducer函数的switch语句中,确保处理所有可能的action类型,并根据需要返回新的state对象。
在创建store时,使用正确的reducer函数。例如,如果您的reducer函数名为appReducer,则应使用以下代码初始化store:
// app.reducer.ts
export function appReducer(state: IAppState, action: Action) {
// ...
}
// app.module.ts
import { StoreModule } from '@ngrx/store';
import { appReducer } from './app.reducer';
@NgModule({
imports: [
StoreModule.forRoot({ appState: appReducer })
]
})
export class AppModule { }
export interface IAppState {
counter: number;
}
// app.reducer.ts
const initialState: IAppState = {
counter: 0
};
export function appReducer(state: IAppState = initialState, action: Action) {
// ...
}
以上是解决Angular Redux,无法将IAppState设置为State的方法之一。