简体   繁体   中英

Angular Error: “is part of the declarations of 2 modules (same)”

Today a very strange error occured:

Uncaught Error: Type DashboardComponent is part of the declarations of 2 modules: DashboardModule and DashboardModule!

That doesn't make any sense to me. There is only one module called DashboardModule and the DashboardComponent is declared and exported only once in this module.

*Update

Here is how the DashboardModule looks like

import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
import { FormsModule } from "@angular/forms";
import { NgbModule } from "@ng-bootstrap/ng-bootstrap";
import { TranslateModule } from "@ngx-translate/core";

import { TypeaheadModule } from "root/shared/typeahead/typeahead.module";
import { AppFormsModule } from "root/shared/forms/forms.module";

import { DynamicComponentService } from "./dynamicComponent.service";
import { DashboardComponent } from "./dashboard.component";
import { DashboardWidgetComponent } from "./widget.component";
import { ModalConfigComponent } from "./modalConfig.component";

import { DashboardService } from "./dashboard.service";

@NgModule({
    imports: [
        CommonModule,
        FormsModule,
        NgbModule,
        TranslateModule,
        TypeaheadModule,
        AppFormsModule],
    exports: [
        DashboardComponent,
        DashboardWidgetComponent,
        ModalConfigComponent],
    declarations: [
        DashboardComponent,
        DashboardWidgetComponent,
        ModalConfigComponent],
    providers: [
        DynamicComponentService,
        DashboardService],
    entryComponents: [
        ModalConfigComponent]
})
export class DashboardModule { }

In the meantime I found the bug:

It was Visual Studio code that decided to import the DashobardModule in a new style. Instead of

import { DashboardModule } from "root/shared/dashboard/dashboard.module";

it started to generate this code

import { DashboardModule } from "root/shared/dashboard/dashboard.module.js";

On other parts of the code the old way still existed. So, angular thought there would exist two different DashboardModules and brought up the error above. Turns out Angular behaves totally correct but I have to find out why VS Code started to generate such a horrible mess...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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