簡體   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