Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import one component from module to component of another module

Tags:

module

angular

I work with angular 6 and i have a media module. I want to import MediaComponent ( exported in MediaModule) but i have this error:

media.module"' has no exported member 'MediaComponent'.

this is the MediaModule declaration:

@NgModule({
  imports: [
    CommonModule,
    MediaRoutingModule,
    MaterialModule
  ],
  declarations: [LayoutComponent, MediaComponent],
  exports: [
    MediaComponent,
    CommonModule
  ]
})

and this is another module declaration:

@NgModule({
  imports: [
    CommonModule,
    MaterialModule,
    FormsModule,
    EM,
    EditorRoutingModule,
    MediaModule
  ],
  declarations: [ListComponent, CreateComponent, AddimageComponent]
})

from component in another module (EditorModule), this is my import:

import { MediaComponent } from '@app/views/media/media.module';

and i got the exception.

Notice: When i use media directive in a component on EditorModule, it work file. for example <app-media></app-media> display the content of MediaComponent

like image 775
hubert Avatar asked Jul 22 '18 12:07

hubert


People also ask

How do I import one component into another component?

1. Using selector as HTML tag. This is widely used way to include child component in other components and you might be already aware of this way. In this selector of child component is used in the parent component i.e. app component in this case, as normal HTML tag.

Can we import module in component Angular?

You can import it directly or from another NgModule that re-exports it. Import FormsModule from @angular/forms if your components have [(ngModel)] two-way binding expressions. Import shared and feature modules when this module's components incorporate their components, directives, and pipes.


2 Answers

make sure media.module.ts exports a the MediaComponent class.

export { MediaComponent } from 'path/to/media.component';

like image 103
Gerald Chifanzwa Avatar answered Sep 19 '22 10:09

Gerald Chifanzwa


You need to export the MediaComponent from the MediaModule in both the Angular sense and in the JavaScript sense, which are different things.

You have already exported the component in the Angular sense by including it the 'exports' section of the module's @NgModule decorator, but you also need to add export {MediaComponent} at the end of the module file so that your import { MediaComponent } from '@app/views/media/media.module'; will work.

Also, you should not be exporting another module from your module - you should remove 'CommonModule' from the MediaModule's 'exports' section.

So, change

@NgModule({
  imports: [
    CommonModule,
    MediaRoutingModule,
    MaterialModule
  ],
  declarations: [LayoutComponent, MediaComponent],
  exports: [
    MediaComponent,
    CommonModule
  ]
})
export class MediaModule {
}

to:

@NgModule({
  imports: [
    CommonModule,
    MediaRoutingModule,
    MaterialModule
  ],
  declarations: [LayoutComponent, MediaComponent],
  exports: [
    MediaComponent
  ]
})
export class MediaModule {
}

export {MediaComponent};
like image 36
GreyBeardedGeek Avatar answered Sep 19 '22 10:09

GreyBeardedGeek