首页 > 解决方案 > Ionic 3 open modal 出现未在入口组件中声明但已存在的错误

问题描述

我在 ionic 3 中有以下模式:

从'@angular/core'导入{ NgModule };从“离子角”导入{ IonicPageModule };从'./login-modal'导入{ LoginModalPage };

@NgModule({
  declarations: [
    LoginModalPage,
  ],
  imports: [
    IonicPageModule.forChild(LoginModalPage),

  ],
  entryComponents: [LoginModalPage]
})
export class LoginModalPageModule {}

我想在 app.component.ts/html 中使用:

import { LoginModalPage } from '../modals/login-modal/login-modal';
import { LoginModalPageModule } from '../modals/login-modal/login-modal.module';
export function init_app(appLoadService: InitializerService) {
  return () => {
      return appLoadService.initializeApp();
  }
}
@NgModule({
  declarations: [
    MyApp,
    HomePage,
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp, {
      preloadModules: true
    }),
    IonicPageModule.forChild(MyApp),
    HttpClientModule,
    LoginModalPageModule
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    GatewayService,
    UserService,
    GlobalVarService,
    AuthGuardService,
    HttpClient,
    InitializerService,
    {
      provide: APP_INITIALIZER,
      useFactory: init_app,
      deps: [InitializerService],
      multi: true
    },

  ]
})
export class AppModule {}

但我收到以下错误:

core.js:1449 错误错误:未捕获(承诺中):错误:未找到 [object Object] 的组件工厂。你把它添加到@NgModule.entryComponents 了吗?错误:未找到 [object Object] 的组件工厂。你把它添加到@NgModule.entryComponents 了吗?

我尝试将其添加到 app.module.ts 的入口组件中,但出现以下错误:

LoginModalModule 在 2 个组件中声明

标签: angularionic-frameworkionic3

解决方案


为了在您的文件中使用它,AppComponent您需要将它添加到您的declarations文件中entryComponentsAppComponent.ts

将此添加到您的AppComponent.ts,

declarations: [
  MyApp,
  HomePage,
  LoginModalPage
]

entryComponents: [
  LoginModalPage
]

将此添加到您的LoginPageModule

exports: [ LoginPageModal ]

根据entryComponents的定义

指定定义此模块时应编译的组件列表。对于这里列出的每个组件,Angular 都会创建一个 ComponentFactory 并将其存储在 ComponentFactoryResolver 中。


推荐阅读