简体   繁体   English

Angular 12,Typecript 4.2:class 列在 NgModule 的声明中,但不是指令、组件或 Z20826A3CB51D6C7D9C219C7F4BF4E5

[英]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.

相关问题 Angular2 - NgModule之外的组件注入 - Angular2 - Component Injection outside of NgModule Angular 更改组件声明位置 - Angular Change Component Declarations Location 用于构建动态 HTML 的最合适的 Angular 组件/指令/管道是什么? - What is the most appropriate Angular component/directive/pipe to use to build dynamic HTML like this? Angular 2组件指令不起作用 - Angular 2 component directive not working 如何在 Angular 2 中动态加载组件,而不在 NgModule 的声明部分列出它们 - How to load components dynamically in Angular 2, without listing them in NgModule's declarations section Angular2 - 在组件 class 中创建多个变量声明与在模板中直接使用 object - Angular2 - creating multiple variable declarations in component class vs directly using object in template Angular2:如何解决Component不是任何NgModule的一部分……? - Angular2: How to resolve Component is not part of any NgModule …? 如何解决错误组件不是任何 NgModule 的一部分或未在 angular 6 中正确导入? - How to solve error Component is not part of any NgModule or not imported correctly in angular 6? 角度指令至零件角度1.5 - Angular Directive to component angular 1.5 Angular 2管道/过滤器,组件变量 - Angular 2 pipe/filter with component variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM