Rodrigo Silva
Rodrigo Silva

Reputation: 387

How can I access root state from lazy loaded modules?

How can I provide root state inside a lazy loaded module?

I have a root state which has the loading state. I also have a user feature state which is loaded in a lazy loaded module. The loading state is in root level because it is suppose to be available for the entire application (a service that handles http request is responsible for dispatching actions that alter loading state). In my user lazy loaded module, I have a component that needs access to the loading state. So, how can I have access to the loading state if in the user module I already provide the user state?

/// user.module.ts

@NgModule({
  imports: [
    ...
    StoreModule.forFeature('user', reducers),
    ...
  ],
  declarations: [
    ...
  ]
})

Upvotes: 3

Views: 499

Answers (1)

DavidZ
DavidZ

Reputation: 441

Your entire app has access to the root state, simply write a selector for it.

export class MyComponent {

  isLoading$: Observable<boolean>;

  constructor(private store: Store<AppState>) {
    this.isLoading$ = this.store.pipe(select(fromRoot.isLoading));
  }

}

Upvotes: 3

Related Questions