12

我正在尝试在我的主页上创建一个弹出框。所以我创建了以下功能..

public presentPopover(myEvent) {
    let popover = this.popoverCtrl.create(TestComponent);
    popover.present({
      ev: myEvent
    });
  }

在我的 homepage.module.ts 中,我添加了 testComponent 作为入口组件。

import { TestModule } from './../../components/test/test.module';
import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { HomePage } from './home';
import { TestComponent } from './../../components/test/test';

@NgModule({
  declarations: [
    HomePage,
  ],
  imports: [
    TestModule,
    IonicPageModule.forChild(HomePage),
  ],
  entryComponents: [
    TestComponent,
  ]
})

但是当我单击该弹出按钮时,我仍然收到此错误。

ERROR Error: Uncaught (in promise): Error: No component factory found for TestComponent. Did you add it to @NgModule.entryComponents?
Error: No component factory found for TestComponent. Did you add it to @NgModule.entryComponents?
    at noComponentFactoryError (core.js:3786)
    at CodegenComponentFactoryResolver.resolveComponentFactory (core.js:3850)
    at PopoverCmp._load (popover-component.js:41)
    at PopoverCmp.ionViewPreLoad (popover-component.js:33)

而且我很困惑为什么要将此添加到条目组件中?

4

1 回答 1

15

您需要将动态创建的组件添加到 @NgModule 中的入口组件

@NgModule({
  ...
  declarations: [
    ...
    Name of your component,
  ],
  entryComponents: [
    ...
    Name of your component,    
  ]
})

注意:在某些情况下,延迟加载的模块下的入口组件将不起作用,作为一种解决方法,将它们放在您的 app.module (root) 中

于 2018-05-28T06:17:29.403 回答