Angular 7- 如何在不同模块中使用同一个组件
创始人
2024-10-16 21:01:20
0

要在不同的模块中使用同一个组件,需要将该组件声明为共享组件,并将其导入到需要使用它的模块中。以下是一个示例解决方法:

首先,创建一个共享组件,例如shared.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-shared',
  template: '

Shared Component

', }) export class SharedComponent {}

然后,在需要使用该共享组件的模块中,将其导入并将其声明为该模块的一个组件。例如,在module1.module.ts中使用共享组件:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { SharedComponent } from './shared.component';

@NgModule({
  declarations: [SharedComponent],
  imports: [CommonModule],
  exports: [SharedComponent]
})
export class Module1Module {}

module2.module.ts中使用共享组件的方法类似:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { SharedComponent } from './shared.component';

@NgModule({
  declarations: [SharedComponent],
  imports: [CommonModule],
  exports: [SharedComponent]
})
export class Module2Module {}

接下来,将这两个模块导入到主模块(通常是app.module.ts)中:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { Module1Module } from './module1.module';
import { Module2Module } from './module2.module';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, Module1Module, Module2Module],
  bootstrap: [AppComponent]
})
export class AppModule {}

最后,在主模块的模板中使用共享组件:


这样,无论是在Module1Module还是在Module2Module中使用app-shared组件,都会显示相同的内容。

相关内容

热门资讯

传递经验!wepoker数据分... 传递经验!wepoker数据分析,wepoker可以开透视吗,必备教程(有挂辅助);致您一封信;亲爱...
信息共享!pokemmo内置修... 信息共享!pokemmo内置修改器,xpoker辅助助手,AI教程(有挂透明);实战中需综合运用上述...
实测教程!wepoker提高好... 实测教程!wepoker提高好牌率,竞技联盟辅助插件,微扑克教程(有挂辅助);实战中需综合运用上述技...
一分钟揭秘!wepoker透视... 一分钟揭秘!wepoker透视脚本免费使用视频,hhpoker有辅助吗,可靠技巧(有挂透视)1、不需...
揭秘真相!wepoker模拟器... 揭秘真相!wepoker模拟器哪个,约局吧开挂神器是真的吗,2025新版技巧(有挂软件);1分钟了解...
一起来探讨!wepoker好友... 此外,数据分析德州(wepoker好友房开挂)辅助神器app还具备辅助透视行为开挂功能,通过对客户w...
科技介绍!pokemmo脚本辅... 科技介绍!pokemmo脚本辅助器,wepoker数据分析工具,微扑克教程(有挂教程)准备好在wep...
重磅来袭!aapoker透视脚... 重磅来袭!aapoker透视脚本下载,wepoker私人定制透视,介绍教程(有挂方法)是一款可以让一...
发现玩家!aapoker怎么开... 《发现玩家!aapoker怎么开辅助器,sohoo poker辅助器,玩家教程(有挂技巧)》 aap...
技巧知识分享!wepoker辅... 技巧知识分享!wepoker辅助插件功能,hhpoker辅助挂,新版2025教程(有挂透明)关于we...