I have two angular modules: main and feature:
Main / root module:
@NgModule({
imports: [
StoreModule.forRoot({router: routerReducer}),
EffectsModule.forRoot([...]),
...
],
declarations: [],
...
})
export class AppModule {}
Feature module:
@NgModule({
imports: [
StoreModule.forFeature('feature', {someValue: someValueReducer}),
EffectsModule.forFeature([...]),
...
],
declarations: [],
...
})
export class FeatureModule {}
I need to access 'feature' slice of data in main module to conditionally display / activate application tab based on data stored in feature
module.
(In other words I need a global / shared piece of state accessible by all modules main and any feature module.)
- Is it possible to do that?
- Is it considered as good practise?
Currently I can't do that since there is no feature
state in main AppState
:
export interface AppState {
router: RouterReducerState<RouterStateUrl>;
}
And typescript indicates error this.store.select('feature')
since there is no feature
key of main store AppState
.
Using selector
: this.store.select(selectFeatureValue)
I am getting runtime error:
export const selectFeatureModule = createFeatureSelector<FeatureState>('feature');
export const selectFeatureValue = createSelector(selectFeatureModule ,
(state: FeatureState) => state.someValue);
I am getting error in main AppComponent:
TypeError: Cannot read property 'someValue' of undefined
setTimeout(() => this.store.select(selectFeatureValue) .subscribe(console.log.bind(console)), 1000);
will get rid of the runtime error, but I would prefer to get rid of thesetTimeout
– Schlenger