Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度:在NgModule entryComponents数组中使用库中的组件时出现问题_Angular - Fatal编程技术网

Angular 角度:在NgModule entryComponents数组中使用库中的组件时出现问题

Angular 角度:在NgModule entryComponents数组中使用库中的组件时出现问题,angular,Angular,我在同一个项目中有一个库和一个展示模块 库模块有一个PopoverService和一个PopoverComponent PopoverService使用PopoverComponent作为角度CDK组件门户构造函数的第一个参数: PopoverService: open(...) { new ComponentPortal( PopoverComponent, // ... ) } Showcase

我在同一个项目中有一个和一个展示模块

库模块有一个PopoverService和一个PopoverComponent

PopoverService使用PopoverComponent作为角度CDK组件门户构造函数的第一个参数:

PopoverService

open(...) {
    new ComponentPortal(
                    PopoverComponent,
                    // ...
    )
}
Showcase模块直接使用PopoverService(但不使用PopoverComponent

Lib模块如下所示:

@NgModule({
    declarations: [PopoverComponent],
    imports: [
        // ... other modules
        OverlayModule
    ],
    providers: [PopoverService],
    exports: [PopoverComponent]
})
export class MyLibModule { }
export * from './lib/my-lib.module';
import { PopoverComponent } from 'my-lib/lib/components/popover/popover.component';
@NgModule({
  // ... other
  imports: [
    // other modules
    MyLibModule
  ],
  entryComponents: [PopoverComponent]
})
export class ShowcaseModule { }
公共api.ts如下所示:

@NgModule({
    declarations: [PopoverComponent],
    imports: [
        // ... other modules
        OverlayModule
    ],
    providers: [PopoverService],
    exports: [PopoverComponent]
})
export class MyLibModule { }
export * from './lib/my-lib.module';
import { PopoverComponent } from 'my-lib/lib/components/popover/popover.component';
@NgModule({
  // ... other
  imports: [
    // other modules
    MyLibModule
  ],
  entryComponents: [PopoverComponent]
})
export class ShowcaseModule { }
展示模块是这样的:

@NgModule({
    declarations: [PopoverComponent],
    imports: [
        // ... other modules
        OverlayModule
    ],
    providers: [PopoverService],
    exports: [PopoverComponent]
})
export class MyLibModule { }
export * from './lib/my-lib.module';
import { PopoverComponent } from 'my-lib/lib/components/popover/popover.component';
@NgModule({
  // ... other
  imports: [
    // other modules
    MyLibModule
  ],
  entryComponents: [PopoverComponent]
})
export class ShowcaseModule { }
但我在调用PopoverService.open()时遇到此错误:


我做错了什么?

我错误地将PopoverComponent添加到了错误的模块中。不得不补充

entryComponents: [PopoverComponent]

在库模块中,而不是在使用Showcase模块中。不过,我不会因为@MikeOne的宝贵建议而删除这个问题

您应该将Pop组件作为单独的导出添加到公共APi中。这样,您就可以在消费模块中导入它,并将其添加到EntryComponents中。作为补充说明:如果您需要您的pop服务始终是单例的,请不要在lib模块中提供它,而是在公共api中单独导出该服务。。。