[英]Angular 12, Typecript 4.2 : The class is listed in the declarations of the NgModule , but is not a directive, a component, or a pipe
I upgraded my app from Angular 11 to 12, and to typescript 4.2.4.我将我的应用程序从 Angular 11 升级到 12,然后升级到 typescript 4.2.4。 When I do ng serve, app fails to compile with the error:当我执行服务时,应用程序无法编译并出现错误:
error NG6001: The class 'ChartComponent' is listed in the declarations of the NgModule 'PrototypeModule', but is not a directive, a component, or a pipe. Either remove it from the NgModule's declarations, or add an appropriate Angular decorator.
41 , ChartComponent
~~~~~~~~~~~~~~~~~~~~~~~~
src/app/chartPage/Chart/chart.component.ts:46:14
46 export class ChartComponent implements OnInit {
~~~~~~~~~~~~~~~~~~~~~~~~
'ChartComponent' is declared here.
This is the code in the PrototypeModule这是 PrototypeModule 中的代码
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { ChartComponent } from './chartPage/Chart/chart.component';
@NgModule({
declarations: [
ChartComponent
]
schemas: [ CUSTOM_ELEMENTS_SCHEMA ],
})
export class ChartPrototypeModule { }
@NgModule({
imports: [ChartPrototypeModule]
, exports: [],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
export class PrototypeModule { }
The ChartComponent is as follows:图表组件如下:
import {Component, Input, ViewEncapsulation, Injectable, OnInit, SimpleChange, NgModule} from '@angular/core';
import { DataLoadService } from '../../services/data-load.service';
@Component({
selector: 'app-chart',
templateUrl: `./chart.component.html`,
styleUrls: ['./chart.component.scss']
, encapsulation: ViewEncapsulation.None
})
@Injectable({
providedIn: 'root'
})
@NgModule({
imports: [SharedModule]
})
export class ChartComponent implements OnInit {
constructor(private data: DataLoadService) {
//constructor code
};
}
I have no leads as to what might be causing the error.对于可能导致错误的原因,我没有任何线索。 How do I fix this?我该如何解决?
So, as @Muhammet Can TONBUL has mentioned you're using the component in a wrong way.因此,正如@Muhammet Can TONBUL 所提到的,您以错误的方式使用该组件。
First of all your component should look something like this:首先,您的组件应如下所示:
@Component({
selector: 'app-chart',
templateUrl: `./chart.component.html`,
styleUrls: ['./chart.component.scss'],
// Use this only if you DON'T want to encapsulate your SCSS
encapsulation: ViewEncapsulation.None
})
export class ChartComponent implements OnInit {
constructor(private data: DataLoadService) { ... };
...
}
The next step is to create a NgModule
like you've already done:下一步是像你已经完成的那样创建一个NgModule
:
@NgModule({
declarations: [
ChartComponent
],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ],
})
export class ChartPrototypeModule { }
So, now you've declared your component and you can use it now but currently only in the other components of the ChartPrototypeModule
.所以,现在您已经声明了您的组件,您现在可以使用它,但目前只能在ChartPrototypeModule
的其他组件中使用。
To change this, you need to export your component in the module as well.要更改这一点,您还需要在模块中导出组件。 This would look something like this:这看起来像这样:
// Introduced an additional array so it is not needed
// to add your components twice
const COMPONENTS = [
ChartComponent
];
@NgModule({
declarations: [
COMPONENTS
],
exports: [
COMPONENTS
],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ],
})
export class ChartPrototypeModule { }
Now you can use your component in each module where you have imported the ChartPrototypeModule
.现在,您可以在导入ChartPrototypeModule
的每个模块中使用您的组件。
You can read more about feature modules here .您可以在此处阅读有关功能模块的更多信息。
Note: If you're using the introduced array COMPONENTS
to reduce redundancy only add the components to it, which you want to use outside of the module.注意:如果您使用引入的数组COMPONENTS
来减少冗余,只需将要在模块外部使用的组件添加到其中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.