简体   繁体   中英

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: [
    ...
  ]
})

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));
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM