Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

typescript - 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

App fails to compile with error

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.

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 {}
    }

question from:https://stackoverflow.com/questions/61308517/angular-compile-error-ng6001-the-class-is-listed-in-the-declarations-of-the-ng

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...