【问题标题】:Ionic 3: no component factory found,did you add it to @NgModule.entryComponents?Ionic 3:未找到组件工厂,您是否将其添加到 @NgModule.entryComponents?
【发布时间】:2019-11-06 01:52:51
【问题描述】:

我创建了一个 Angular 库,其中包含一些组件和一个用于导出所有组件的模块。该库的模块如下:

import { NgModule, ModuleWithProviders } from '@angular/core';
import { FirstComponent } from './first';
import { IonicModule } from 'ionic-angular';
import { ProviderTest } from '../../providers/test/test'
import { SecondComponent } from '../second/second';
import { ThirdComponent } from '../third/third';

@NgModule({
    imports: [
        IonicModule
    ],
    declarations: [
        FirstComponent,
        SecondComponent,
        ThirdComponent
    ],
    exports: [
        FirstComponent,
        SecondComponent,
        ThirdComponent
    ],
    entryComponents: [
        FirstComponent,
        SecondComponent,
        ThirdComponent
    ]
})
export class ComponentModule {
    static forRoot(): ModuleWithProviders {
        return {
            ngModule: ComponentModule,
            providers: [ProviderTest]
        };
    }
}

我的问题是当我尝试在我的项目中使用这个库时,我得到了这个:

Error: Uncaught (in promise): Error: No component factory found for SecondComponent. Did you add it to @NgModule.entryComponents?

我已经看到相关问题,他们说将我的 ComponentModule 添加到我想要使用此库的模块的声明中,但如果我这样做,我会收到此错误:

Error: Uncaught (in promise): Error: Unexpected module 'ComponentModule' declared by the module 'TestPageModule'. Please add a @Pipe/@Directive/@Component annotation.

TestPageModule的模块现在是这样的:

@NgModule({
  declarations: [
    TestPage,
    ComponentModule
  ],
  imports: [
    IonicPageModule.forChild(TestPage),
    ComponentModule
  ],
})
export class TestPageModule {}

有谁知道问题出在哪里? SecondComponent 是一个模态,以防万一它有任何用处。此外,这只发生在使用延迟加载时。

谢谢。

【问题讨论】:

    标签: angular ionic-framework ionic3 node-modules


    【解决方案1】:

    Error: Uncaught (in promise): Error: Unexpected module 'ComponentModule' declared by the module 'TestPageModule'. Please add a @Pipe/@Directive/@Component annotation.

    declarations 仅适用于pipecomponentdirective。您应该只将ComponentModule 添加到您希望使用它的模块的导入部分。这样做,模块的所有导出组件都将在TestPageModule 中可用:

    @NgModule({
      declarations: [
        TestPage
      ],
      imports: [
        IonicPageModule.forChild(TestPage),
        ComponentModule
      ],
    })
    export class TestPageModule {}
    

    【讨论】:

    • 这就是我从一开始就拥有它的方式,但它给了我我提到的第一个错误,它没有检测到 SecondComponent,它是一个模态并在运行时创建
    • 如果您从declarations 中删除TestPageModule 中的ComponentModule 并将其保留在imports 中,您是否仍然没有SecondComponentTestPageModule 中可用?
    • 没错,我得到了问题中提到的工厂错误
    • 您是否尝试在控制台中重新启动应用程序?
    • 是的,它的工作方式是如果我也将它添加到 add.module.ts ,但我认为这不应该是这种方式......
    【解决方案2】:

    如果你使用延迟加载和组件,你应该在app.module.ts 中导入你的组件,并且你应该导入使用这个组件的页面 所以你应该从其他声明中删除页面并将它们导入app.module.ts

    【讨论】:

    • 这不是让延迟加载完全没用吗?
    • 好吧,只添加使用组件的页面而不是所有页面
    猜你喜欢
    • 2018-04-09
    • 2019-01-30
    • 2019-07-25
    • 2018-01-06
    • 2018-08-07
    • 2017-09-25
    • 2019-11-05
    • 2019-12-06
    相关资源
    最近更新 更多