how to use parent module component in child module component in angular2

后端 未结 2 1919
攒了一身酷
攒了一身酷 2021-02-12 11:38

I have header that should be used in both child and parent module.that was imported and used in parent module but when try to import and using in child component it showing erro

2条回答
  •  清歌不尽
    2021-02-12 11:41

    You should create a shared module with the components you want to use, export these components, and import the shared module in your other modules (parent and child for your case).

    Shared module:

    import { NgModule } from '@angular/core';
    import { CommonModule } from '@angular/common';
    import { SharedComponent1 } from "./SharedComponent1";
    import { SharedComponent2 } from "./SharedComponent2";
    
    @NgModule({
    imports: [
        CommonModule
    ],
    declarations: [
        SharedComponent1,
        SharedComponent2
    ],
    exports: [
        SharedComponent1,
        SharedComponent2
    ]
    })
    export class SharedModule {}
    

    Using Shared module:

    import { NgModule }       from '@angular/core';
    import { CommonModule }   from '@angular/common';
    ...
    import { SharedModule } from './SharedModule';
    
    @NgModule({
    imports: [
        CommonModule,
        ...
        SharedModule
    ],
    declarations: [
        ...
    ],
    providers: [
        ...
    ]
    })
    export class AppModule{}
    

提交回复
热议问题