angular – 使用Core模块中的组件

前端之家收集整理的这篇文章主要介绍了angular – 使用Core模块中的组件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是Angular2的新手,我在使用im(ex)移植模块时遇到了一些困难.

根据官方风格指南,CoreModule是:

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

这是否意味着所有从Core模块导出的东西只能在仅导入它的模块中使用?在某些子模块中没有办法提供这些东西吗?我创建了虚拟项目来测试这种行为:

虚拟组件:

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

核心模块:

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

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

export class CoreModule {}

App模块(根模块):

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

Home模块声明home组件:

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

当我尝试运行此代码时出现此错误

'dummy-component' is not a known element

解决方法

如果您想要其他模块中的东西,那么将它放入共享模块(功能模块). CoreModule不是正确的地方.

是的,您需要将模块导入到要使用该模块中的组件,指令或管道的每个模块.

猜你在找的Angularjs相关文章