Angular - using forRoot({}) config in loadChildren() to configure a lazy-loaded module

荒凉一梦 提交于 2021-01-01 02:16:25

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/59875936/angular-using-forroot-config-in-loadchildren-to-configure-a-lazy-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!