Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 error.I mean how to use common header for both parent and child module

Uncaught (in promise): Error:

Type HeaderComponent is part of the declarations of 2 modules: AppModule and ProviderModule! Please consider moving HeaderComponent to a higher module that imports AppModule and ProviderModule.

You can also create a new NgModule that exports and includes HeaderComponent then import that NgModule in AppModule and ProviderModule.

like image 850
j dileep Avatar asked Jun 10 '17 08:06

j dileep


People also ask

How do you use the parent module component in a child module?

You should export the components of the parent module, you want to use in the child module. Then import the parent module in the child module. Show activity on this post. It would have been great , if you can share the code and the specified the error you are getting.

How do you use the parent component variable in a child component?

If you want to pass data from the parent component to the child component, you need to use two things: @Input and property binding. In this example, we set in the child component a variable named childExample , which is a string. We set Angular's @Input decorator in front of the variable.


2 Answers

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{}
like image 67
ST7 Avatar answered Oct 17 '22 17:10

ST7


I have modified ST7s answer.

You should export the components of the parent module, you want to use in the child module. Then import the parent module in the child module.

Parent module (exporting shared components):

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

Child module (importing parent module):

import { NgModule }       from '@angular/core';
import { CommonModule }   from '@angular/common';
...
import { ParentModule } from '../ParentModule';

@NgModule({
imports: [
    CommonModule,
    ...
    ParentModule
],
declarations: [
    ...
],
providers: [
    ...
]
})
export class ChildModule{}
like image 23
Paul Avatar answered Oct 17 '22 16:10

Paul