1

I want to have a loaded module configurable. In theory, I want a module that is its own bigger application do receive some configuration. To show / load certain elements depending on the config the main application sends down.

I used that with other modules but those where not lazyLoaded. I tried and searched around and have not seen the usage of something like:

 children: [
      {
        path: 'application-module',
        loadChildren: () => import('@application-module').then(m => m.ApplicationModule.forRoot({
         foo:'bar'
        }))
      }
    ]

Is that even possible? If I use it I get the error error: Error: No NgModule metadata found for '[object Object]'. That is common when the module you load is not defined.

Or am I on the wrong track and there is a better solution for that? Thank you

wenzel
  • 403
  • 4
  • 14
  • Hey! Did you find the solution for this? I want to pass a service that has the configuration for the module but it doesn't work... :/ – Jovem May 15 '20 at 10:18
  • I managed to solve this here : https://stackoverflow.com/a/76287978/2294082 Basically : You can define a shared module accepting some parameters in the `forRoot`, and define one module in each app, calling the shared module. Those "app-modules" can then be called using lazy-loading, and provide the required parameters to your shared module – Adrien Dos Reis May 19 '23 at 14:24

2 Answers2

2

I suppose one thing you can try, because what you're doing now won't work. Reason being that loadChildren expects a class with the @module decorator.

What you are giving it is a static function.

What you could try is to do this:

children: [
      {
        path: 'application-module',
        loadChildren: () => import('@application-module').then(m => { m.ApplicationModule.forRoot({foo: 'bar'}); return m.ApplicationModule; } )
    }))
      }
    ]

I have no idea if that will work though.

Another option is to go for a service or through routing in the receiving lazyloaded module route.

Inge Olaisen
  • 69
  • 1
  • 6
  • So you are absolutely right! - So with the changes you suggested the compilation works. But it looks like that the module will not provide the configuration. I will investigate If I did a mistake somewhere else. Thank you for you help! – wenzel Jan 24 '20 at 12:51
  • 1
    Another solution, and I think this is better is to use a service to accomplish that. Since you need to trigger the route. The parent module can provide the service, then you just tell the service which config you want and inject that service into the parent module in the constructor. Then fetch the config that you want. – Inge Olaisen Jan 25 '20 at 01:04
0

If the Module is in your code, just change the function return

@Module({})
export class ApplicationModule {
  static forRoot(): typeof ApplicationModule {

    return this;
  }
}
Mor Bargig
  • 198
  • 1
  • 7