使用 Angular 添加新组件时出错

我从 Angular 开始,但是我在添加新组件时遇到了问题:

我已经开始在角一个新的项目,ng new toto没有错误的话,我用添加新的组件ng generate component menu没有错误,我删除我的所有app.component.html,我加MenuComponent<MenuComponent></MenuComponent>

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { MenuComponent } from './menu/menu.component';

    @NgModule({
      declarations: [
        AppComponent,
        MenuComponent
      ],
      imports: [
        BrowserModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }

menu.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-menu',
  templateUrl: './menu.component.html',
  styleUrls: ['./menu.component.css']
})
export class MenuComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

应用程序组件.html

<MenuComponent></MenuComponent>

我的 Angular CLI 版本:

Angular CLI: 6.0.0
Node: 10.0.0
OS: linux x64
Angular: 6.0.0
... animations, cli, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.6.0
@angular-devkit/build-angular     0.6.0
@angular-devkit/build-optimizer   0.6.0
@angular-devkit/core              0.6.0
@angular-devkit/schematics        0.6.0
@ngtools/webpack                  6.0.0
@schematics/angular               0.6.0
@schematics/update                0.6.0
rxjs                              6.1.0
typescript                        2.7.2
webpack                           4.6.0

我的问题是为什么这个简单的事情不起作用?

我的错误

 Error: Template parse errors:
'MenuComponent' is not a known element:
1. If 'MenuComponent' is an Angular component, then verify that it is part of this module.
2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("[ERROR ->]<MenuComponent></MenuComponent>
"): ng:///AppModule/AppComponent.html@0:0

在此先感谢您的帮助

萨吉塔兰

我相信你的selectorforMenuComponent应该是不同的。

使用selector您所拥有的menu.component.ts而不是使用组件名称本身。就像是,

<app-menu> </app-menu>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在asp.net core spas中使用angular cli生成新组件时出错

Angular 7-使用ngTemplateOutlet构建组件库时出错

为Angular5添加组件时出错

Angular2,尝试添加新模块时出错

尝试在 Angular 9 中使用共享模块时出错,使我的组件无法识别我的材料模块

尝试在动态创建的组件angular 5.1中使用服务时出错

无法使用打字稿向我的angular 2应用添加新组件

使用Leaflet&Angular 6创建L.control()的新实例时出错

Angular:传递@Input值以覆盖按钮文本并使用ngClass添加类时出错

使用npm更新Angular时出错

尝试使用 Angular 创建数据时出错

在 Angular 中使用 Http 模块时出错

使用angular 4构建aot时出错

Angular 6“在使用工作区之前,需要先加载它。” 尝试生成新组件时

使用Angular,如何在单击时动态添加组件?(不仅一个)

Angular 4:无法使用Angular CLI生成新组件

使用 *ngIf 时保留 Angular 组件变量

使用 Angular 在表中添加新行

Angular - 使用 FormControlArray 作为数据源时向 mat-table 添加新行

使用Angular在HTML表中使用组件动态添加行

Angular 5添加组件的新实例

在模板中使用组件时向组件传递 angular 属性

使用尝试使用angular2 / Http时出错

当新数据添加到 sessionStorage 时,如何更新我的 angular 组件?

使用ng serve时在Angular类中编译时出错

使用CakePHP的Auth组件登录时出错

在导出的组件中使用“ElementRef”时出错

使用ART中的组件文本时出错

如何使用CLI在Angular 4中创建新组件