简体   繁体   中英

Angular 8 - Displaying components inside of other components

Problem:

To display (custom) Navigation component ( titled topnav ) in app.component.html

app.module.ts

   import { TopnavComponent } from './topnav/topnav.component';

   @NgModule({
         declarations: [
            AppComponent,
            TopnavComponent,
         ]
    })

*edited to show AppComponent inside of declarations

app.component.html

 <topnav></topnav>

SOLUTION

<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() {
  }

}

*edited to show topnav.component.ts

Not sure what I am missing here. I attempted to try different directives, importing and exporting in different ways. I know this is a basic idea behind Angular but having trouble and has been documented with Angular 2 but their solutions were not resolving in Angular 8.

Still getting this error:

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

When adding to imports:[] in app.module.ts, from my understanding only takes modules based on the error I was getting.

You are using the wrong selector. Replace app.component.html with this:

 <app-topnav></app-topnav>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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