我已经在化简器中添加了不变性检查,只是激活了一个:
StoreModule.forRoot(reducers, {
runtimeChecks: {
strictStateImmutability: true,
// strictActionImmutability: true,
// strictStateSerializability: true --> Sucks, f.e. can't serialize objects
// strictActionSerializability: true, --> Sucks too
},
}),
这里没有什么太花哨的,但是似乎只要将任何runtimeChecks设置为true,商店就会捕获ANY属性,而不仅仅是状态中的那些。
例如,我有一个小的登录组件:
public formGroup: FormGroup;
public isLoggingIn = false;
public constructor(
private formGroupBinder: RxFormGroupBindingService,
private formBuilder: LogInFormBuilderService,
private store: Store<IAppState>) { }
public ngOnInit(): void {
console.log('ngOnInit');
this.formGroup = this.formBuilder.buildFormGroup();
}
public logIn(): void {
this.isLoggingIn = true;
const request = new LoginRequest();
this.formGroupBinder.bindToModel(this.formGroup, request);
this.store.dispatch(new LogInAction(request));
}
public get canLogIn(): boolean {
console.log('canLogIn: ' + !!this.formGroup);
return !this.formGroup.invalid && !this.isLoggingIn;
}
一旦激活商店严格检查,就会在ngOnInit之前调用“ canLogin”属性,从而导致“ formgroup is undefined”错误。控制台显示以下内容:
足够有趣的是,我没有发现关于不变性检查的任何提示。代码有什么问题吗?还是我只是失去了组件生命周期挂钩的安全性?