我在项目中一直遵循此Medium article中的概念,并在stackblitz中进行了复制,以便成功地懒惰加载 feature模块,而无需安装路由器需求。 (角度10)
逐行逻辑是有意义的。但是,当在按钮按下时调用lazyLoad()
并且存在错误时,这两种实现都不能解析模块组件的实例。
应用组件:
public loadFeature() {
// Dynamic import, activate code splitting and on demand loading of feature module
import('./lazy/lazy.module').then(({ LazyFeatureModule }) => {
// Compile the module
this.compiler.compileModuleAsync(LazyFeatureModule).then(moduleFactory => {
// Create a moduleRef, resolve an entry component, create the component
const moduleRef = moduleFactory.create(this.injector);
const componentFactory = moduleRef.instance.resolveComponent();
const { instance } = this.container.createComponent(componentFactory);
// you have to manually call ngOnChanges for dynamically created components
instance.ngOnChanges(); // ERROR: Property 'ngOnChanges' does not exist on type 'LazyComponent'.
});
});
}
错误:未被捕获(承诺):错误:找不到LazyComponent的组件工厂。您是否将其添加到@ NgModule.entryComponents中?