简体   繁体   English

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

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

I have a Notification Popup declare in app module.我在应用程序模块中有一个通知弹出窗口声明。 This Notification Popup is custom so it have custom fields marked as @Inputs.此通知弹出窗口是自定义的,因此它具有标记为 @Inputs 的自定义字段。 I want to be able to use this component in another component that requires notification popup functionality.我希望能够在另一个需要通知弹出功能的组件中使用这个组件。


@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 { }

I tried importing and exporting but without succes.我尝试导入和导出但没有成功。

Make a separate folder which includes notification-popup component and module file make module like创建一个单独的文件夹,其中包含通知弹出组件和模块文件

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

Declare PopupNotificationComponent in PopupNotificationModule and whenever you wish to use in another module just import PopupNotificationModule and that's it.在 PopupNotificationModule 中声明 PopupNotificationComponent ,每当您希望在另一个模块中使用时,只需导入 PopupNotificationModule 即可。

first you have to add PopupNotificationComponent in declarations and export section of PopupNotificationModule like that首先你必须像这样在PopupNotificationModule的声明和导出部分添加PopupNotificationComponent

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

then you have to import PopupNotificationModule in app module import section like that然后你必须像这样在应用程序模块导入部分导入PopupNotificationModule

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

now you have import PopupNotificationModule in another module where you want to use PopupNotificationComponent现在您已经在要使用PopupNotificationComponent的另一个模块中导入PopupNotificationModule

in your case it is CarModule在您的情况下,它是CarModule

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

})

export class CarModule { }

now you can use PopupNotificationComponent selector in CarModule components.现在您可以在CarModule组件中使用PopupNotificationComponent选择器。

i hope this will work for you我希望这对你有用

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

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