Angular reuse same lazy load module for multiple root paths

后端 未结 1 908
天涯浪人
天涯浪人 2021-02-19 23:17

I\'ve splitted my app into two modules: one with main basic functionality and other with less-used features like account settings, faq pages and more.

What I\'m trying t

1条回答
  •  独厮守ぢ
    2021-02-20 00:01

    To create an instance of a component in a lazy loaded module without the router, this snippet could help:

    class LazyLoader {
        constructor(private _injector: Injector,
                    private _moduleLoader: NgModuleFactoryLoader) {
        }
    
        public loadLazyModule() {
            this._moduleLoader.load('./modules/settings/settings.module#SettingsModule')
                .then((moduleFactory: NgModuleFactory) => {
                    const moduleRef = moduleFactory.create(this._injector);
    
                    // Here you need a way to reference the class of the component you want to lazy load
                    const componentType = (moduleFactory.moduleType as any).COMPONENT_CLASS;
                    const compFactory = moduleRef.componentFactoryResolver.resolveComponentFactory(componentType);
                    const componentRef = container.createComponent(compFactory);
    
                    // Instance of the lazy loaded component
                    componentRef.instance 
                })
        }
    }
    

    0 讨论(0)
提交回复
热议问题