我有使用angular-cli生成的Angular 2(版本2.0.0 - 最终版)app .
当我创建一个组件并将其添加到 AppModule
's declarations array it'时,它都可以正常工作 .
我决定将组件分开,所以我创建了一个 TaskModule
和一个组件 TaskCard
. 现在我想在 AppModule
( Board
组件)的一个组件中使用 TaskCard
.
的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:
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-board文件夹)上找到
我错过了什么?我需要做什么才能在 BoardComponent
中使用 TaskCardComponent
?
6 回答
SOLVED HOW TO USE A COMPONENT DECLARED IN A MODULE IN OTHER MODULE.
基于Royi Namir的解释(非常感谢你) . 在使用延迟加载时,在任何其他模块中重用模块中声明的组件缺少部分 .
1st:导出包含它的模块中的组件:
第二:在您要使用TaskCardComponent的模块中:
像这样,第二个模块导入第一个导入和导出组件的模块 .
当我们在第二个模块中导入模块时,我们需要再次导出它 . 现在我们可以使用第二个模块中的第一个组件 .
请注意,为了创建所谓的"feature module",您需要在其中导入
CommonModule
. 因此,您的模块初始化代码将如下所示:更多信息请点击此处:https://angular.io/guide/ngmodule#create-the-feature-module
这里的主要规则是:
The selectors which are applicable during compilation of a component template are determined by the module that declares that component, and the transitive closure of the exports of that module's imports.
所以,尝试导出它:
What should I export?
The minute you create a new module, lazy or not, any new module and you declare anything into it, that new module has a clean state (正如Ward Bell在_368818中所说)
Angular为每个
@NgModule
创建 transitive module .这个模块 collects directives that either imported from another module(if transitive module of imported module has exported directives) or declared in current module .
当角度编译属于模块
X
的模板时,它将被用于 X.transitiveModule.directives 中收集的那些指令 .https://github.com/angular/angular/blob/4.2.x/packages/compiler/src/jit/compiler.ts#L250-L251
这种方式根据上图
YComponent
不能在模板中使用ZComponent
因为Transitive module Y
的directives
数组不包含ZComponent
因为YModule
没有进口ZModule
其传递模块包含ZComponent
exportedDirectives
阵列英寸在
XComponent
模板,我们可以使用ZComponent
因为Transitive module X
具有指令数组,其中包含ZComponent
因为XModule
进口模块(YModule
),其出口模块(ZModule
),出口指令ZComponent
在
AppComponent
模板中我们不能使用XComponent
因为AppModule
导入XModule
但XModule
不导出XComponent
.也可以看看
why lazy loaded module has to import commonModule? Angular 2
Angular Module FAQs
What is difference between declarations, providers and import in NgModule
无论你想从另一个模块使用什么,只需将其放入 export array 即可 . 像这样-
一个很好的方法是从
NgModuleFactory
加载模块,你可以通过调用这个来加载另一个模块中的模块:我从here得到了这个 .
你必须
export
来自NgModule
: