繁体   English   中英

'<selector> ' 不是已知元素</selector>

[英]'<selector>' is not a known element

在我继续这个问题之前:我已经检查了具有相同标题的其他问题,并且我已经遵循了给出的建议。 这些都没有帮助,所以我决定提出一个新问题。

我有一个模块和多个组件的 angular 项目。 仪表板组件使用了显然无法识别的欢迎组件。

该模块具有以下代码:

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

import { HomeComponent } from './home/home.component';
import { DashboardComponent } from './dashboard/dashboard.component';
import { FrameComponent } from './frame/frame.component';
import { WelcomeComponent } from './welcome/welcome.component';
import { CardComponent } from './card/card.component';
import { AlertComponent } from './alert/alert.component';

@NgModule({
  declarations: [
    FrameComponent,
    HomeComponent,
    CardComponent,
    WelcomeComponent,
    AlertComponent
  ],
  imports: [
    BrowserModule,
    RouterModule.forRoot([
      {
        path: '',
        component: HomeComponent
      },
      {
        path: 'dashboard',
        component: DashboardComponent
      }
    ])
  ],
  exports: [CardComponent, WelcomeComponent, AlertComponent],
  providers: [],
  bootstrap: [FrameComponent]
})
export class AppModule { }

如您所见,我已经导入了 WelcomeComponent,声明了它并导出了它。 WelcomeComponent 包含以下代码:

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

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

  @Input()
  message: string;
  welcomeMessage: string;

  constructor() { }

  ngOnInit(): void {
  }
}

在这里,您可以看到 app-welcome 作为选择器。 它似乎连接正确。 但是当我在dashboard.component.html中使用它时,如示例所示

<app-welcome [message]="message" [welcomeMessage]="welcomeMessage"></app-welcome>

我收到以下错误:

错误:src/app/dashboard/dashboard.component.html:18:5 - 错误 NG8001:'app-welcome' 不是已知元素:1. 如果 'app-welcome' 是 Angular 组件,则验证它是本模块的一部分。 2. 如果“app-welcome”是 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以禁止显示此消息。

其他错误与welcome 和welcomeMessage 相关,因为无法识别组件。

我尝试重新启动服务器,将其作为 --prod 运行。 我尝试了我发现的一切。 有谁知道这会导致什么?

仪表板组件未在您的 AppModule 中声明,这就是问题所在。 在这种情况下,您也不需要导出组件,只需声明它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM