【问题标题】:The component scope of n NgModule?n NgModule 的组件范围?
【发布时间】:2019-02-28 10:38:21
【问题描述】:

我会用一个例子来问这个问题。我们有这个模块:

  import { NgModule } from '@angular/core';
...
  import { TodoComponent } from './todo.component';
...

  @NgModule({
    imports: [CommonModule, ReactiveFormsModule],
    exports: [TodosComponent, TodosFiltersComponent],
    declarations: [TodoComponent, TodosComponent, TodosFiltersComponent, TodosPageComponent]
  })
  export class TodosModule {}

我假设TodoComponentTodoPageComponent 只能在declarations 数组中列出的其他声明的组件中使用,并且这些是它们唯一可以使用的组件,因为它们没有被导出?只要确保我正确理解了 Angular 组件范围......

【问题讨论】:

  • 是的,一个模块的组件只有在没有导出的情况下才能被同一个模块的组件使用

标签: angular typescript scope angular-components


【解决方案1】:

要在应用程序中使用组件,您必须将其添加到任何模块的声明中。

要在其基本模块之外的其他模块中使用组件,您必须导出该组件。

考虑像组件的“命名空间”这样的模块。

提供者是完全不同的故事。

【讨论】:

    猜你喜欢
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多