Angular 编译错误:NG6001:该类列在 NgModule 'AppModule' 的声明中,但不是指令、组件或管道 [英] Angular Compile Error: NG6001: The class is listed in the declarations of the NgModule 'AppModule', but is not a directive, a component, or a pipe

查看:246
本文介绍了Angular 编译错误:NG6001:该类列在 NgModule 'AppModule' 的声明中,但不是指令、组件或管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

App fails to compile with error

错误 NG6001:类 NavigationMenuItemComponent 列在 NgModule AppModule 的声明中,但不是指令、组件或管道.要么从 NgModule 的声明中删除它,要么添加一个合适的 Angular 装饰器.

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 方法

The error goes away when I remove the constructor with parameters. How can I resolve this whiles maintaining the constructor that has parameters, because I want to use to initialise a list of the component without having to call set methods for each member in the list

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.热重载好像不能添加组件.

You have to run npm install when creating new components. Hot reload doesn't seem to be able to add the component.

这篇关于Angular 编译错误:NG6001:该类列在 NgModule 'AppModule' 的声明中,但不是指令、组件或管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆