angular - 尽管声明,“组件选择器”不是已知元素?
问题描述
在我的 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 还不够?
解决方案
推荐阅读
- java - How to Assert same response from list in Junit using hamcrest
- javascript - 合并数组以形成具有重复项的列表
- python - How to fix error with decimal numbers in Python?
- visual-c++ - How to activate CMakeSettings.json variables in VS2019
- reactjs - React useEffect updating state but JSX code not updating
- intellij-idea - Clion快捷键上下移动“切换器”
- python-3.x - Is there an elegant solution to concatenating Dataframes as fixed element in a list?
- r - Lines and secondary y-axis are not displayed in plotly barplot with overlying line chart
- excel - How to get results based on multiple selection from the list box in excel using VBA?
- javascript - 如何阻止 VS Code 建议我从转译文件中导入代码?