Evg*_*rov 171 typescript angular-module angular
我有使用angular-cli生成的Angular 2(版本2.0.0 - 最终版)app.
当我创建一个组件并将其添加到AppModule
声明数组时,它一切都很好,它可以工作.
我决定将组件分开,所以我创建了TaskModule
一个组件TaskCard
.现在我想用TaskCard
在的组成部分之一AppModule
(的Board
成分).
的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 { }
Run Code Online (Sandbox Code Playgroud)
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{}
Run Code Online (Sandbox Code Playgroud)
整个项目可在https://github.com/evgdim/angular2(kanban-board文件夹)上找到
我错过了什么?我必须做的,用TaskCardComponent
的BoardComponent
?
yur*_*zui 332
这里的主要规则是:
在编译组件模板期间适用的选择器由声明该组件的模块以及该模块的导入的传递的传递闭包确定.
所以,尝试导出它:
@NgModule({
declarations: [TaskCardComponent],
imports: [MdCardModule],
exports: [TaskCardComponent] <== this line
})
export class TaskModule{}
Run Code Online (Sandbox Code Playgroud)
我应该出口什么?
导出其他模块中的组件应该能够在其模板中引用的可声明类.这些是你的公共课程.如果不导出类,则它保持私有,仅对此模块中声明的其他组件可见.
你创建一个新模块,懒惰与否,任何新模块并向其声明任何内容的那一刻,新模块都处于干净状态(正如Ward Bell在 https://devchat.tv/adv-in-angular/119中所述) -aia-avoid-common-pitfalls-in-angular2)
Angular 为每个s 创建传递模块@NgModule
.
此模块收集从另一个模块导入的指令(如果导入模块的传递模块具有导出的指令)或在当前模块中声明.
当angular compiles模板属于模块时,X
它使用那些在X.transitiveModule.directives中收集的指令.
compiledTemplate = new CompiledTemplate(
false, compMeta.type, compMeta, ngModule, ngModule.transitiveModule.directives);
Run Code Online (Sandbox Code Playgroud)
https://github.com/angular/angular/blob/4.2.x/packages/compiler/src/jit/compiler.ts#L250-L251
这种方式根据上图
YComponent
不能ZComponent
在其模板中使用因为directives
数组Transitive module Y
不包含ZComponent
因为YModule
没有导入ZModule
其传递模块包含ZComponent
在exportedDirectives
数组中.
在XComponent
模板中我们可以使用ZComponent
因为Transitive module X
有指令数组包含ZComponent
因为
XModule
导入module(YModule
)导出ZModule
导出指令的module()ZComponent
在AppComponent
模板中,我们不能使用XComponent
因为AppModule
导入XModule
但XModule
不导出XComponent
.
也可以看看
mxi*_*xii 36
你必须export
从你的NgModule
:
@NgModule({
declarations: [TaskCardComponent],
exports: [TaskCardComponent],
imports: [MdCardModule],
providers: []
})
export class TaskModule{}
Run Code Online (Sandbox Code Playgroud)
小智 22
(角度2-角度7)
组件只能在单个模块中声明。为了使用另一个模块中的组件,您需要执行两个简单的任务:
第一个模块:
有一个组件(我们称它为“ ImportantCopmonent”),我们想在第二个模块的页面中重复使用。
@NgModule({
declarations: [
FirstPage,
ImportantCopmonent // <-- Enable using the component html tag in current module
],
imports: [
IonicPageModule.forChild(NotImportantPage),
TranslateModule.forChild(),
],
exports: [
FirstPage,
ImportantCopmonent // <--- Enable using the component in other modules
]
})
export class FirstPageModule { }
Run Code Online (Sandbox Code Playgroud)
第二模块:
通过导入FirstPageModule,重用“ ImportantCopmonent”
@NgModule({
declarations: [
SecondPage,
Example2ndComponent,
Example3rdComponent
],
imports: [
IonicPageModule.forChild(SecondPage),
TranslateModule.forChild(),
FirstPageModule // <--- this Imports the source module, with its exports
],
exports: [
SecondPage,
]
})
export class SecondPageModule { }
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
138870 次 |
最近记录: |