Angular 编译错误 : NG6001: The class is listed in the declarations of the NgModule 'AppModule' , 但不是指令、组件或管道

标签 angular typescript

应用程序无法编译并出现错误

error NG6001: The class 'NavigationMenuItemComponent' is listed in the declarations of the NgModule 'AppModule', but is not a directive, a component, or a pipe. Either remove it from the NgModule's declarations, or add an appropriate Angular decorator.



当我删除带有参数的构造函数时,错误消失了。
如何在维护具有参数的构造函数的同时解决此问题,因为我想用来初始化组件列表,而不必为列表中的每个成员调用 set 方法
import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-navigation-menu-item', templateUrl: './navigation-menu-item.component.html', styleUrls: ['./navigation-menu-item.component.scss'] }) export class NavigationMenuItemComponent implements OnInit { static readonly ID_PREFIX: string = 'sidebar-menuitem-'; static readonly ICON_CLASS_PREFIX: string = 'mdi mdi-'; constructor(id: string, iconClass: string) { this._id = NavigationMenuItemComponent.ID_PREFIX + id; this._iconClass = NavigationMenuItemComponent.ICON_CLASS_PREFIX + iconClass; } //constructor() {} private _id: string; private _iconClass: string; get id() { return this._id; } get iconClass() { return this._iconClass; } set id(id: string) { this._id = NavigationMenuItemComponent.ID_PREFIX + id; } set iconClass(iconClass) { this._iconClass = NavigationMenuItemComponent.ID_PREFIX + iconClass; } ngOnInit(): void {} }

最佳答案

你必须运行 npm install创建新组件时。热重载似乎无法添加组件。

关于Angular 编译错误 : NG6001: The class is listed in the declarations of the NgModule 'AppModule' , 但不是指令、组件或管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61308517/

相关文章:

javascript - 我想在 Angular 6 中更改页面内容而不重新加载

javascript - 如何从 Angular 中的另一个组件滚动到一个组件?

typescript - 错误 TS2339 : Property 'coord' does not exist on type 'Mixed'

javascript - typescript 错误 TS2339 : Property 'webkitURL' does not exist on type 'Window'

javascript - Angular 2 应用程序无法在 IE、MAC Safari 中运行

angular - 如何在 ionic 3 中单击图标时显示输入字段

javascript - 可以使用 AngularJS 和 Typescript 进行 Plunker 吗?

javascript - 模型更改后 Angular View 不更新

angular - 如何刷新路线的解析数据

typescript - Jest 和 TypeScript 的不完整 stub