Angular 7.2-“组件是2个模块的声明的一部分”,但不是

时间:2019-02-18 13:35:06

标签: angular

从Angular 5.2更新到Angular 7.2,

我有一个仅在模块中只使用一次的组件

但是当我“ ng build --prod”项目时,出现此错误

ERROR in : Type GraphComponent in C:/myproject/src/app/widgets/graph/graph.component.ts is part of the declarations of 2 modules: 

DashBoardModule in C:/myproject/src/app/pages/dashBoard/dashBoard.module.ts and 
DashBoardModule in C:/myproject/src/app/pages/dashboard/dashBoard.module.ts

请注意,两个模块都是相同的

我不能指责这里出了什么问题,这是我导入的组件:

import { Component, OnInit, Input,OnChanges,SimpleChange } from '@angular/core';

@Component({
    selector: 'my-graph',
    templateUrl: './graph.component.html',
    styleUrls: ['./graph.component.css'],
})
export class GraphComponent implements OnInit,OnChanges {
...
}

这是我在模块中导入组件的方式

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { ReactiveFormsModule, FormBuilder } from '@angular/forms';

import { DashBoardComponent } from './dashBoard.component';
import { DashBoardService } from './dashBoard.service';
import { DashBoardRoutingModule } from './dashBoard.routing';

import { LoadingOverlayModule } from '../../widgets/loadingOverlay/loadingOverlay.module';
import { DataTableModule } from '../../widgets/dataTable/dataTable.module';
import { ERFormModule } from '../../widgets/erform/erform.module';

import { GraphComponent } from '../../widgets/graph/graph.component';

import { PageTitleService } from '../../widgets/pageTitle/pageTitle.service';

@NgModule({
    imports: [
        CommonModule,
        FormsModule,
        ReactiveFormsModule,
        DataTableModule,
        ERFormModule,
        DashBoardRoutingModule,
        LoadingOverlayModule
    ],
    declarations: [
        DashBoardComponent,
        GraphComponent
    ],
    providers: [DashBoardService, FormBuilder,PageTitleService],
})
export class DashBoardModule {}

这里是DashBoardRoutingModule

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { DashBoardComponent } from './dashBoard.component';

import { AuthGuardService } from '../../auth/authGuard.service';

const dashboardRoutes: Routes = [
    {
        path: 'dashBoard',
        component: DashBoardComponent,
        pathMatch: 'full',
        canActivate: [AuthGuardService],
    }
];
@NgModule({
    imports: [
        RouterModule.forChild(dashboardRoutes)
    ],
    exports: [
        RouterModule
    ]
})
export class DashBoardRoutingModule {}

这应该(并且正在)工作

在模块中导入组件时需要考虑一些新的考虑吗?

致谢

2 个答案:

答案 0 :(得分:0)

可能是您有任何未使用或已归档的模块,其中也声明了DashboardComponent

因为我看到有两个模块dashboarddashBoard。那就是错误的意思。

答案 1 :(得分:0)

C:/ myproject / src / app / pages / dashBoard /dashBoard.module.ts中的

DashBoardModule和 C中的DashBoardModule:/ myproject / src / app / pages / 仪表板 /dashBoard.module.ts

dashBoard和仪表板不同,将它们更改为只有一个版本(文件夹的名称)