Angular模块导入的范围是什么

时间:2017-07-17 14:31:30

标签: angular

如果我有三个模块

  1. 的AppModule
  2. SharedModule
  3. ItemModule
  4. 在SharedModule中我有一个PaginatorComponent,导出如下:

    shared.module.ts

    import { NgModule } from '@angular/core';
    import { CommonModule } from '@angular/common';
    
    import { PaginatorComponent } from './paginator/paginator.component';
    
    @NgModule({
      imports: [
          CommonModule
      ],
      declarations: [
          PaginatorComponent
      ],
      providers: [],
      exports: [
          PaginatorComponent
      ]
    })
    export class SharedModule { }
    

    然后我使用行

    将共享模块导入到我的AppModule中

    app.module.ts

    import { SharedModule } from './shared/shared.module';
    
    imports: [
          ...
          SharedModule,
          ...
    

    问题;我的第三个“ItemModule”是否可以自动访问PaginatorComponent,还是需要直接导入第3个模块?目前我得到了一般错误的变体:

    Can't bind to 'totalItems' since it isn't a known property of 'app-paginator'.
    1. If 'app-paginator' is an Angular component and it has 'totalItems' input, then verify that it is part of this module.
    

    非常感谢。

2 个答案:

答案 0 :(得分:2)

答案可以在 https://medium.com/@cyrilletuzi/understanding-angular-modules-ngmodule-and-their-scopes-81e4ed6f7407感谢@Rama

简而言之,答案是

  1. 如果为组件导入模块,则需要在需要它们的每个模块中导入模块;

  2. 如果为服务导入模块,则只需在第一个应用模块中导入一次。

答案 1 :(得分:2)

当我开始使用Angular 2 + /

时,这是我误解的

您需要重新导入每个模块中使用的每个组件。

以您为例,如果您需要在PaginatorComponent中使用ItemModule,则需要将SharedModule导入ItemModule以使其正常运行。 (因为SharedModule导出PaginatorComponent)

服务/提供商不是这种情况。如果您注入服务,则每个子组件都可以访问它。