无法以模态动态加载或延迟加载组件

时间:2018-12-06 04:02:39

标签: angular lazy-loading ionic4

我试图通过仅提供类名而不是类来通过Ionic ModalController加载组件,

启动模态时出现以下错误

'LocationComponent'

但是,如果我改用该类,则可以加载它。

演示:https://stackblitz.com/edit/github-lazyload?file=src%2Fapp%2Fhome%2Fhome.page.ts

No component factory found for LocationComponent. Did you add it to @NgModule.entryComponents?

home.page.ts

我在openModal() { this._modalCtrl.create({ component: 'LocationComponent' // Error: No component factory found for LocationComponent. // Did you add it to @NgModule.entryComponents? // component: LocationComponent }).then(modal => modal.present()); } 中定义了entryComponents: [LocationComponent]

如何让它延迟加载?

1 个答案:

答案 0 :(得分:1)

Angular将组件工厂保留在Map词典中,其中键是组件类型而不是字符串

this._factories.set(factory.componentType, factory);

因此使用:

component: LocationComponent

应该为您工作。

Stackblitz Example