Angular多个页面引入同一个组件报错The Component ‘MyComponentComponent‘ is declared by more than one NgModule怎么办?

简介: Angular多个页面引入同一个组件报错The Component ‘MyComponentComponent‘ is declared by more than one NgModule怎么办?

有一天,我写了一个自信满满的自定义组件myComponent,在多个页面import使用了,结果控制台给我来这个

我特么裤子都脱了,你给我来这个提示是几个意思


仔细一看 The Component 'MyComponentComponent' is declared by more than one NgModule


什么鬼?说我的组件被多个模块使用?搞什么飞机,我就是要多个页面使用呀!!!

通常出现上面这种报错都是因为使用了懒加载路由(类似下面的)

const routes: Routes = [
...
  {
    path: 'first',
    loadChildren: () => import('./com/first/first.module').then(m => m.FirstModule),//异步加载、惰性加载、懒加载
  }, 
...
]

于是乎查阅官方文档发现一个shared.module.ts的东东

首先找一个干净的文件夹创建一个

shared.module.ts

import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { MyComponentComponent } from 'src/app/my-component/my-component.component';
// 这里加入自定义公共组件----------------------------------------
let declarations = [
  MyComponentComponent,
];
@NgModule({
  imports: [CommonModule,], //公共组件依赖的第三方组件可以在此处引入
  declarations,
  exports: declarations,
})
export class SharedModule { }

去需要使用相关公共组件的页面Module文件里面

first.module.ts

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { SharedModule } from '../../shared.module';
import { FirstComponent } from './first.component';
@NgModule({
  imports: [
    SharedModule,//这里引入公共组件模块(共享特性模块)
    RouterModule.forChild([{ path: '', component: FirstComponent }]),//这句不加不会生效
  ],
  declarations: [FirstComponent,]
})
export class FirstModule { }

first.component.html

first
<app-my-component></app-my-component>

second.module.ts

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { SharedModule } from '../../shared.module';
import { SecondComponent } from './second.component';
@NgModule({
  imports: [
    SharedModule,//这里引入公共组件模块(共享特性模块)
    RouterModule.forChild([{ path: '', component: SecondComponent }]),//这句不加不会生效
  ],
  declarations: [SecondComponent,]
})
export class SecondModule { }

second.component.html

second
<app-my-component></app-my-component>

my-component.component.html

<h1>我特么终于没报错啦!</h1>

路由自己配置好,直接访问http://localhost:4200/first

访问http://localhost:4200/second


相关文章
|
22天前
|
JavaScript
Angular使用@Input和@Output实现父子组件互相传参(类似Vue的props和this.emit)
Angular使用@Input和@Output实现父子组件互相传参(类似Vue的props和this.emit)
|
4月前
|
Web App开发 UED 开发者
谈谈企业级 Angular 应用的二次开发 - 基于 Angular Component 替换的 Extensibility 支持案例介绍
谈谈企业级 Angular 应用的二次开发 - 基于 Angular Component 替换的 Extensibility 支持案例介绍
40 1
|
5月前
【超实用】Angular如何修改当前页面网页浏览器url后面?param1=xxx&param2=xxx参数(多用于通过浏览器地址参数保存用户当前操作状态的需求),实现监听url路由切换、状态变化。
【超实用】Angular如何修改当前页面网页浏览器url后面?param1=xxx&param2=xxx参数(多用于通过浏览器地址参数保存用户当前操作状态的需求),实现监听url路由切换、状态变化。
【超实用】Angular如何修改当前页面网页浏览器url后面?param1=xxx&param2=xxx参数(多用于通过浏览器地址参数保存用户当前操作状态的需求),实现监听url路由切换、状态变化。
|
22天前
【超实用】Angular如何修改当前页面网页浏览器url后面?param1=xxx&param2=xxx参数(多用于通过浏览器地址参数保存用户当前操作状态的需求),实现监听url路由切换、状态变化。
【超实用】Angular如何修改当前页面网页浏览器url后面?param1=xxx&param2=xxx参数(多用于通过浏览器地址参数保存用户当前操作状态的需求),实现监听url路由切换、状态变化。
|
7月前
|
JavaScript 数据安全/隐私保护 开发者
Angular Component Class 成员属性默认的访问权限控制
Angular Component Class 成员属性默认的访问权限控制
65 0
|
4月前
|
前端开发 JavaScript
Angular 组件模版代码里使用 ngIf 进行条件渲染的例子
Angular 组件模版代码里使用 ngIf 进行条件渲染的例子
22 0
|
4月前
|
JavaScript
Angular Component 内 set 关键字的使用
Angular Component 内 set 关键字的使用
24 0
|
5月前
|
应用服务中间件 nginx
Angular打包构建项目服务器运行runtime.js、polyfills.js、vendor.js报错net::ERR_ABORTED 404 (Not Found),build修改为相对路径./
Angular打包构建项目服务器运行runtime.js、polyfills.js、vendor.js报错net::ERR_ABORTED 404 (Not Found),build修改为相对路径./
|
5月前
快速创建Angular组件并定义传参、绑定自定义事件的方法
快速创建Angular组件并定义传参、绑定自定义事件的方法
|
6月前
|
前端开发 JavaScript 测试技术
什么是 Angular 的 Custom component
什么是 Angular 的 Custom component
31 1

热门文章

最新文章