与开发模式相比,构建模式下的Angular NgRx怪异行为

时间:2019-02-28 10:53:05

标签: angular ngrx reactive ngrx-store

我在Angular 7项目中使用NgRx lib。
为了根据屏幕显示组件,我使用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;
    });
  }

Coordiantes组件:需要显示帮助按钮的屏幕示例

export class CoordinatesComponent implements OnInit {
...
  constructor(
    private store: Store<HelpState>,
    ...
  ) {}

  ngOnInit() {
    this.store.dispatch({type: '1'});
    ...
  }

  ...
}

HelpState界面

interface HelpState {
  message: string;
}

NgRx的AppModule配置

import {StoreModule} from '@ngrx/store';
...

@NgModule({
...
imports: [
...
StoreModule.forRoot({message: simpleReducer}),
...]
})

0 个答案:

没有答案