Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using component from Core module

Tags:

angular

I'm Angular2 newbie and I have some difficulties with im(ex)porting modules.

According to official styleguide CoreModule is:

CoreModule - import once when the app starts and never import anywhere else.

Does this mean that all those exported stuff from Core module will be only available in the Module where it is imported only? There is no way to make those stuff available in some child module? I created dummy project just to test this behavior:

Dummy component:

@Component({
  selector: 'dummy-component',
  template: '<h4>Dummy component</h4>',
})
export class DummyComponent {}

Core module:

import { DummyComponent } from './dummy.component';

@NgModule({
  declarations : [DummyComponent],
  exports : [DummyComponent]
})

export class CoreModule {}

App module(root module):

@NgModule({
  declarations: [AppComponent,],
  imports: [BrowserModule, CoreModule, HomeModule, RoutingModule],
  exports : [CoreModule],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Home module declares home component:

@Component({
  selector: 'home-component',
  template: 
  `
    <h2>Home component</h2>
    <dummy-component></dummy-component>
  `
})
export class HomeComponent {}

When I try to run this code I got this error:

'dummy-component' is not a known element
like image 958
Ivan Pandžić Avatar asked Feb 09 '17 19:02

Ivan Pandžić


1 Answers

If you want stuff in other modules, then put it into a shared module (feature module). CoreModule is not the right place.

Yes you need to import a module to every module where you want to use components, directives, or pipes from that module.

like image 144
Günter Zöchbauer Avatar answered Oct 06 '22 12:10

Günter Zöchbauer