简体   繁体   English

组件是 Angular 中 2 个模块声明的一部分

[英]Component is part of the declarations of 2 modules in Angular

I have created a component called commonmod.component.ts that I am including in two other modules (abc and def).我创建了一个名为commonmod.component.ts的组件,我将其包含在其他两个模块(abc 和 def)中。

abc.module.ts abc.module.ts

import { commonmod } from '../commonmod/commonmod.component';
@NgModule({
  declarations: [
    commonmod
  ]
})

def.module.ts def.module.ts

import { commonmod } from '../commonmod/commonmod.component';
@NgModule({
  declarations: [
    commonmod
  ]
})

When I redirect one page in abc module to another page in def module, it is throwing me following error.当我将 abc 模块中的一个页面重定向到 def 模块中的另一个页面时,它抛出以下错误。

ERROR Error: Uncaught (in promise): Error: Type commonmod is part of the declarations of 2 modules: abcand def.错误错误:未捕获(承诺):错误:类型 commonmod 是 2 个模块声明的一部分:abcand def。 Please consider moving commonmod to a higher module that imports abc and def.请考虑将 commonmod 移动到导入 abc 和 def 的更高模块。 You can also create a new NgModule that exports and includes commonmodthen import that NgModule in abcand def: Error: Type commonmod is part of the declarations of 2 modules.您还可以创建一个新的 NgModule,它导出并包含 commonmod,然后在 abcand def 中导入该 NgModule:错误:类型 commonmod 是 2 个模块声明的一部分。 abc and def. abc 和 def。 Please consider moving commonmodto a higher module that imports abcand def.请考虑将 commonmod 移动到导入 abcand def 的更高模块。 You can also create a new NgModule that exports and includes commonmod then import that NgModule in abc and def.您还可以创建一个新的 NgModule,它导出并包含 commonmod,然后在 abc 和 def 中导入该 NgModule。

A component can be declared in one and only one module.一个组件只能在一个模块中声明。 If you try to declare it in more than one modules you'll get this error如果你试图在多个模块中声明它,你会得到这个错误

Error: Type... is part of the declarations of 2 (or more) modules:错误:类型...是 2 个(或更多)模块声明的一部分:

The solution to this problem is pretty simple.这个问题的解决方案非常简单。 If you need to use a component in more than one modules then add it to the exports array of the module that declared it.如果您需要在多个模块中使用一个组件,则将其添加到声明它的模块的导出数组中。

So lets say we have a component named GreetingsComponent thats is declared in a module TestModule and I want to use it in AppComponent that is declared in AppModule .假设我们有一个名为GreetingsComponent的组件,它在模块TestModule中声明,我想在AppModule 中声明的 AppComponent使用它。 I'll simply add it in the exports array of the TestModule like this我将像这样简单地将它添加到TestModule导出数组中

import {NgModule} from '@angular/core';
import {GreetingComponent} from './greeting.component';
@NgModule({
declarations:[GreetingComponent],
exports:[GreetingComponent]
})
export class TestModule
{
}

Now as AppModule has imported TestModule and this way all constructs whether Components, Directive, Pipes etc that are in the exports array of the TestModule shall be automatically available to the AppModule.现在,由于AppModule已经导入TestModule ,并且通过这种方式所有构造是否在TestModule导出数组中的组件、指令、管道等应自动对 AppModule 可用。

AppModule.ts AppModule.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import {TestModule} from './test.module';
import { AppComponent } from './app.component';
@NgModule({
  imports:      [ BrowserModule, FormsModule, TestModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

Now you can simply use GreetingsComponent in the AppComponent现在您可以简单地在 AppComponent 中使用 GreetingsComponent

  <greetings></greetings>

A working StackBlitz here.这里有一个工作的StackBlitz

Cheers.干杯。

You can't declare a component in more than 1 module.您不能在多个模块中声明一个组件。 If both modules need it, you need to declare/export your component in a third module and imports this one in abc & def.如果两个模块都需要它,您需要在第三个模块中声明/导出您的组件,并在 abc 和 def 中导入该组件。

@NgModule({
  imports:      [ MainModule ]
})
export class AbcModule { }

@NgModule({
  imports:      [ MainModule ]
})
export class DefModule { }

@NgModule({
  declarations: [ CommonMod ],
  exports:    [ CommonMod ]
})
export class MainModule { }

This may also occur if you are using HMR (Hot Module Replacement) with Angular 9+.如果您在 Angular 9+ 中使用 HMR(热模块替换),也可能会发生这种情况。 The Angular team is working on it. Angular 团队正在努力解决这个问题。

See this issue for more details and updates.有关更多详细信息和更新,请参阅此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM