使用另一个模块中的组件

问题描述:

我有使用angular-cli生成的Angular 2.0.0应用程序.

I have Angular 2.0.0 app generated with angular-cli.

当我创建一个组件并将其添加到AppModule的声明数组时,一切都很好,它可以工作.

When I create a component and add it to AppModule's declarations array it's all good, it works.

我决定将组件分开,因此我创建了TaskModule和组件TaskCard.现在,我想在AppModule的一个组件(Board组件)中使用TaskCard.

I decided to separate the components, so I created a TaskModule and a component TaskCard. Now I want to use the TaskCard in one of the components of the AppModule (the Board component).

AppModule:

AppModule:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import { BoardComponent } from './board/board.component';
import { LoginComponent } from './login/login.component';

import { MdButtonModule } from '@angular2-material/button';
import { MdInputModule } from '@angular2-material/input';
import { MdToolbarModule } from '@angular2-material/toolbar';

import { routing, appRoutingProviders} from './app.routing';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';

import { UserService  } from './services/user/user.service';
import { TaskModule } from './task/task.module';


@NgModule({
  declarations: [
    AppComponent,
    BoardComponent,// I want to use TaskCard in this component
    LoginComponent,
    PageNotFoundComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    MdButtonModule,
    MdInputModule,
    MdToolbarModule,
    routing,
    TaskModule // TaskCard is in this module
  ],
  providers: [UserService],
  bootstrap: [AppComponent]
})
export class AppModule { }

TaskModule:

TaskModule:

import { NgModule } from '@angular/core';
import { TaskCardComponent } from './task-card/task-card.component';

import { MdCardModule } from '@angular2-material/card';

@NgModule({
  declarations: [TaskCardComponent],
  imports: [MdCardModule],
  providers: []
})
export class TaskModule{}

整个项目可在 https://github.com/evgdim/angular2 (kanban-板文件夹)

The whole project is available on https://github.com/evgdim/angular2 (kanban-board folder)

我想念什么?在BoardComponent中使用TaskCardComponent怎么办?

What am I missing? What do I have to do to use TaskCardComponent in BoardComponent?

此处的主要规则是:

在组件模板的编译过程中适用的选择器由声明该组件的模块以及该模块的导入的导出的可传递闭包确定.

因此,请尝试将其导出:

So, try to export it:

@NgModule({
  declarations: [TaskCardComponent],
  imports: [MdCardModule],
  exports: [TaskCardComponent] <== this line
})
export class TaskModule{}

我应该导出什么?

导出应声明的类,其他模块中的组件应为 能够在其模板中引用.这些是您的公开课. 如果您不导出课程,则该课程将保持私有状态,仅对其他人可见 该模块中声明的组件.

Export declarable classes that components in other modules should be able to reference in their templates. These are your public classes. If you don't export a class, it stays private, visible only to other component declared in this module.

创建新模块(无论是否懒惰),任何新模块并在其中声明任何内容的那一刻,该新模块就处于干净状态 (如Ward Bell在 https://devchat.tv/adv -in-angular/119-aia-aviating-common-pitfalls-in-angular2 )

Angular为每个@NgModule创建传递模块.

Angular creates transitive module for each of @NgModules.

此模块收集从另一个模块导入的指令(如果导入模块的可传递模块已导出指令)或在当前模块中声明.

当属于模块X的angular编译模板时,将使用在 X.transitiveModule.directives 中收集的那些指令.

When angular compiles template that belongs to module X it is used those directives that had been collected in X.transitiveModule.directives.

compiledTemplate = new CompiledTemplate(
    false, compMeta.type, compMeta, ngModule, ngModule.transitiveModule.directives);

https://github.com/angular/angular/blob/4.2.x/packages/compiler/src/jit/compiler.ts#L250-L251

按照上面的图片这种方式

This way according to the picture above

  • YComponent不能在其模板中使用ZComponent,因为Transitive module Ydirectives数组不包含ZComponent,因为YModule尚未导入ZModule,其传递模块包含exportedDirectives数组中的c11>.

  • YComponent can't use ZComponent in its template because directives array of Transitive module Y doesn't contain ZComponent because YModule has not imported ZModule whose transitive module contains ZComponent in exportedDirectives array.

XComponent模板中,我们可以使用ZComponent,因为Transitive module X具有包含ZComponent的指令数组,因为 XModule导入模块(ZModule)的模块(YModule)导出指令ZComponent

Within XComponent template we can use ZComponent because Transitive module X has directives array that contains ZComponent because XModule imports module (YModule) that exports module (ZModule) that exports directive ZComponent

AppComponent模板中,我们不能使用XComponent,因为AppModule导入XModule,但XModule不导出XComponent.

Within AppComponent template we can't use XComponent because AppModule imports XModule but XModule doesn't exports XComponent.

另请参见

  • why lazy loaded module has to import commonModule? Angular 2

Angular模块常见问题解答

Angular Module FAQs