繁体   English   中英

在另一个模块中使用在 app 模块中声明的组件

[英]Use component declared in app module in another module

我在应用程序模块中有一个通知弹出窗口声明。 此通知弹出窗口是自定义的,因此它具有标记为 @Inputs 的自定义字段。 我希望能够在另一个需要通知弹出功能的组件中使用这个组件。


@NgModule({
  declarations: [],
  imports: [
    CommonModule,
    TranslateModule.forChild(),
  ]
})
export class PopupNotificationModule { }
@NgModule({
  declarations: [
    AppComponent,
    PopupNotificationComponent 
  ],
  imports: [
    PopupNotificationModule
  ],

})

export class AppModule 
@NgModule({
  declarations: [
    CarListComponent,
  ],
  imports: [
  ],

})

export class CarModule { }

我尝试导入和导出但没有成功。

创建一个单独的文件夹,其中包含通知弹出组件和模块文件

@NgModule({
  declarations: [PopupNotificationComponent],
  imports: [
    CommonModule,
    TranslateModule.forChild(),
  ]
})
export class PopupNotificationModule { }

在 PopupNotificationModule 中声明 PopupNotificationComponent ,每当您希望在另一个模块中使用时,只需导入 PopupNotificationModule 即可。

首先你必须像这样在PopupNotificationModule的声明和导出部分添加PopupNotificationComponent

@NgModule({
  declarations: [PopupNotificationComponent],
  imports: [
    CommonModule,
    TranslateModule.forChild(),
  ],
  exports: [PopupNotificationComponent],
})
export class PopupNotificationModule { }

然后你必须像这样在应用程序模块导入部分导入PopupNotificationModule

    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        PopupNotificationModule,
        CarModule 
      ],
    
    })
    
    export class AppModule

现在您已经在要使用PopupNotificationComponent的另一个模块中导入PopupNotificationModule

在您的情况下,它是CarModule

@NgModule({
  declarations: [
    CarListComponent,
  ],
  imports: [
    PopupNotificationModule 
  ],

})

export class CarModule { }

现在您可以在CarModule组件中使用PopupNotificationComponent选择器。

我希望这对你有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM