刚开始玩 ngxs 并遇到了障碍。我不知道我做错了什么,但我收到“状态未定义”错误。
行动:
const prefix = "[Tutorial]";
export class AddTutorial {
static readonly type = `${prefix} Add Tutorial`;
constructor(public payload: string) { }
}
状态:
export class TutorialStateModel {
tutorial: any[]
}
@State<TutorialStateModel>({
name: 'tutorial',
defaults: {
tutorial: [],
}
})
export class TutorialState {
@Action(AddTutorial)
addTutorial({ getState, patchState }: StateContext<TutorialStateModel>, { payload }: AddTutorial) {
const state = getState();
patchState({
tutorial: [...state.tutorial, payload]
});
}
}
错误指向:const state = getState();
零件:
ngOnInit() {
this.store.dispatch(new AddTutorial('Redux'));
}