使用另一个模块中的组件

角度的 TypeScript

武藏

2020-05-28

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

当我创建一个组件并将其添加到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 { }

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(看板文件夹)

我想念什么?我有什么做用TaskCardComponentBoardComponent

第4239篇《使用另一个模块中的组件》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

5个回答
飞羽Jim 2020.05.28

一种很棒的方法是从加载模块NgModuleFactory,您可以通过调用以下命令将模块加载到另一个模块中:

constructor(private loader: NgModuleFactoryLoader, private injector: Injector) {}

loadModule(path: string) {
    this.loader.load(path).then((moduleFactory: NgModuleFactory<any>) => {
        const entryComponent = (<any>moduleFactory.moduleType).entry;
        const moduleRef = moduleFactory.create(this.injector);
        const compFactory = moduleRef.componentFactoryResolver.resolveComponentFactory(entryComponent);
        this.lazyOutlet.createComponent(compFactory);
    });
}

我从这里得到的

番长樱梅 2020.05.28

无论您想在另一个模块中使用什么,只需将其放在export array中即可像这样-

 @NgModule({
  declarations: [TaskCardComponent],
  exports: [TaskCardComponent],
  imports: [MdCardModule]
})
武藏 2020.05.28

(角度2-角度7)

组件只能在单个模块中声明。为了使用另一个模块中的组件,您需要执行两个简单的任务:

  1. 将组件导出到另一个模块中
  2. 将另一个模块导入当前模块

第一个模块:

有一个组件(我们称它为“ 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 { }

第二模块:

通过导入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 { }
三千曜 2020.05.28

您必须export从您的NgModule

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

这里的主要规则是:

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

因此,尝试将其导出:

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

我应该出口什么?

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

创建新模块(无论是否懒惰),任何新模块并在其中声明任何内容的那一刻,该新模块都处于干净状态(如Ward Bell在 https://devchat.tv/adv-in-angular/119中所述) -aia-避免常见的陷阱(在angular2中

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

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

当属于模块的angular编译模板时,将X使用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

在此处输入图片说明

根据上图这样

  • YComponent无法ZComponent在其模板中使用,因为不包含的directives数组,因为尚未导入其传递模块包含数组中。Transitive module YZComponentYModuleZModuleZComponentexportedDirectives

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

  • AppComponent模板中,我们不能使用XComponent,因为AppModule进口XModuleXModule没有出口XComponent

也可以看看

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android