新建一个共享模块:如shareModule, 并在该模块下新建HelloComponent组件html
再建两个模块: module1, module2来共享shareModule中的HelloComponent组件app
第一步: 在shareModule 的shareModule.ts 文件中找到 exports: [], 并导入HelloComponent组件exports: [HelloComponent],component
若是没有exports,手动加上就好了。htm
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {HelloComponent} from '../hello/hello.component';
@NgModule({
imports: [
CommonModule
],
declarations: [HelloComponent], //HelloComponent 必须成为shareModule的一个组件
exports: [HelloComponent]
})
export class ShareModule { }
第二步: 分别在module1和module2的module1.ts,module2.ts文件下的引入ShareModulimport { NgModule } from '@angular/core';io
import { CommonModule } from '@angular/common';
@NgModule({
imports: [
CommonModule,
ShareModule
],
declarations: [],
exports: []
})
export class Module1Module { }
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({
imports: [
CommonModule,
ShareModule
],
declarations: [],
exports: []
})
export class Module2Module { }
最后: 直接在html中插入hello组件便可,不须要在declarations 增长HelloComponent
如: module1的页面
<app-hello></app-hello>