angular - 使用核心模块中的组件

标签 angular

我是 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 {}

应用模块(根模块):

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

主页模块声明主页组件:

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

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

'dummy-component' is not a known element

最佳答案

如果你想要其他模块中的东西,那么把它放到一个共享模块(功能模块)中。 CoreModule 不是正确的地方。

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

关于angular - 使用核心模块中的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42145705/

相关文章:

angular - ionInput 和 ionChange 之间的显着差异

javascript - Protractor "Error while waiting for Protractor to sync with the page"浏览 Angular 站点

angular - combineLatest 是否因为 behaviorSubject 而没有触发?

Angular 2.0.1 AsyncPipe 不适用于 Rx Subject

angular - 如何通过 systemjs 将 Bootstrap 添加到 Angular 2

javascript - Angular 5 - 每次迭代缩进

angular - 将电容器相机结果转换为 Blob

angular - 如何对依赖于元素高度的组件进行单元测试( Angular 为 5)?

html - 以 Angular 评估表达式两次

用于散列 Assets 和资源文件夹的文件名的 Angular 构建过程