【问题标题】:Angular 8 - Displaying components inside of other componentsAngular 8 - 在其他组件内显示组件
【发布时间】: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:[] 时,据我了解,仅根据我遇到的错误获取模块。

【问题讨论】:

  • AppComponentdeclarations 数组中的位置
  • 我在复制代码时省略了 - 它就在那里。编辑以显示真实代码。
  • 你能告诉我们topnav组件的代码吗?
  • 添加了 topnav 组件。组件中的 HTML 文件现在只是一个表示“测试”的字符串。我删除了所有其他格式以显示组件。
  • 您使用了错误的选择器。在app.component.html 中应该是&lt;app-topnav&gt;&lt;/app-topnav&gt;

标签: angular typescript components


【解决方案1】:

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

 <app-topnav></app-topnav>

【讨论】:

    猜你喜欢
    • 2020-09-16
    • 1970-01-01
    • 2017-09-02
    • 2018-01-22
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2021-04-28
    • 2018-05-17
    相关资源
    最近更新 更多