【发布时间】:2019-09-30 16:51:19
【问题描述】:
问题:
在app.component.html
中显示(自定义)导航组件(titled topnav)app.module.ts
import { TopnavComponent } from './topnav/topnav.component';
@NgModule({
declarations: [
AppComponent,
TopnavComponent,
]
})
*已编辑以在声明中显示 AppComponent
app.component.html
<topnav></topnav>
解决方案
<app-topnav></app-topnav>
topnav.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-topnav',
templateUrl: './topnav.component.html',
styleUrls: ['./topnav.component.css']
})
export class TopnavComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
*编辑显示 topnav.component.ts
不确定我在这里缺少什么。我尝试尝试不同的指令,以不同的方式导入和导出。我知道这是 Angular 背后的一个基本想法,但遇到了麻烦,并且已在 Angular 2 中记录,但他们的解决方案在 Angular 8 中没有解决。
仍然出现此错误:
1. If 'topnav' 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. ("
<div class="container">
[ERROR ->]<topnav></topnav>
</div>
"): ng:///AppModule/AppComponent.html@4:3
当添加到 app.module.ts 中的 imports:[] 时,据我了解,仅根据我遇到的错误获取模块。
【问题讨论】:
-
AppComponent在declarations数组中的位置 -
我在复制代码时省略了 - 它就在那里。编辑以显示真实代码。
-
你能告诉我们
topnav组件的代码吗? -
添加了 topnav 组件。组件中的 HTML 文件现在只是一个表示“测试”的字符串。我删除了所有其他格式以显示组件。
-
您使用了错误的选择器。在
app.component.html中应该是<app-topnav></app-topnav>。
标签: angular typescript components