4

I have a question regarding lazy loading angular feature modules which are also sub states of NGXS state.

For example, I have one parent module and two children modules. I want to lazy load those two children modules. My NGXS store looks like the following:

{
  parent: {
    children1: {
      ...
    },
    children2: {
      ...
    }
  }
}

Since my parent module's state has the following code, sub-level feature module lazy loading is not working with the error saying child state not found. I am not importing two children modules for lazy-loading.

@State<ParentModel>({
  ...
  children: [Child1State, Child2State]
})

Does anyone know how to deal with this situation?

2 Answers 2

0

I don't believe you can assign states from a lazy loaded modules directly as children of another module. Those states won't exist until the module those states reside in loads, leading to the error you are getting.

You should be able to create feature state in the lazy loaded module that extends the parent state. That should add the newly loaded states to the existing state model.

See: https://ngxs.gitbook.io/ngxs/advanced/lazy

Sign up to request clarification or add additional context in comments.

Comments

0

For standalone setups here is what you'll need to do. Please comment any recommendations.

export default [
  {
    path: '',
    component: ParentComponent,
    providers: [importProvidersFrom(NgxsModule.forFeature([ParentState, ChildState]))],
    children: [
      { path: '', redirectTo: 'something', pathMatch: 'full' },
      { path: 'something', component: SomeComponent },
    ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.