首页 > 解决方案 > 尽管声明,“组件选择器”不是已知元素?

问题描述

在我的 AppModule 中,我从拥有一个 ListComponent 过渡到创建一个 ListModule,其中包含对 ListComponent 的声明和一个新生成的 ListItemComponent。在 ListComponent 的模板中,我使用了选择器“app-list-item”,它是 ListItemComponent 组件的选择器,并在列表模块中声明。即便如此,我还是得到了错误:'app-list-item' 不是已知元素:1. 如果 'app-list-item' 是一个 Angular 组件,那么验证它是这个模块的一部分。”不确定是什么我失踪了。

我想要它,以便 ListComponent html 文件可以调用它在其模块中声明的 ListItem 组件选择器。

这是我的代码:

列表项组件.ts

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

@Component({
  selector: 'app-list-item',
  templateUrl: './list-item.component.html',
  styleUrls: ['./list-item.component.scss']
})

export class ListItemComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

列表模块.ts

import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import { ListComponent } from './list.component';
import {ListItemComponent} from './list-item/list-item.component';

@NgModule({
    imports:[ CommonModule ],
    declarations:[ListComponent, ListItemComponent],
    exports:[ListComponent]
})

export class ListModule{}

list-component.html(发生错误的地方):

<app-list-item> </app-list-item>

应用模块.ts:

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

什么可能导致错误?如果 ListComponent 是唯一在其 html 文件中使用 ListItemComponent 选择器的组件,为什么在 List 模块中声明 ListItemComponent 还不够?

标签: angularangular-moduleangular8

解决方案


推荐阅读