NgRx
lib在Angular 7项目中。
为了根据屏幕显示组件,我使用store保存
state
它在开发模式下完美工作:
ng serve
但是一旦我使用
ng build --base-href''
我发现了一个不同的行为:一些不需要显示的组件确实在这里!
有人经历过这种行为吗?找到了解决方案?
这是我的代码:
减速器
export function simpleReducer(state: string = 'activated', action: Action) {
switch (action.type) {
case '1':
return state = 'activated'
case '0':
return state = 'disactivated'
default:
return state;
}
}
收割台组件
*ngIf="isHelpActivated === 'activated'"
ngOnInit() {
this.store.select('message').subscribe(value => {
this.isHelpActivated = value;
});
}
坐标分量
:需要显示“帮助”按钮的屏幕示例
export class CoordinatesComponent implements OnInit {
...
constructor(
private store: Store<HelpState>,
...
) {}
ngOnInit() {
this.store.dispatch({type: '1'});
...
}
...
}
interface HelpState {
message: string;
}
NgRx的AppModule配置
import {StoreModule} from '@ngrx/store';
...
@NgModule({
...
imports: [
...
StoreModule.forRoot({message: simpleReducer}),
...]
})