繁体   English   中英

Angular 8 - 显示其他组件内部的组件

[英]Angular 8 - Displaying components inside of other components

问题:

在 app.component.html 中显示(自定义)导航组件(标题为 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:[] 时,据我了解,仅根据我遇到的错误获取模块。

您使用了错误的选择器。 将 app.component.html 替换为:

 <app-topnav></app-topnav>

暂无
暂无

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

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