简体   繁体   中英

Why does my navbar component not work in Angular?

I am very new to Angular. In my Angular app I have a navbar folder in src/app/components/navbar and my navbar.component.ts file looks like this:

navbar.component.ts

@Component({
  selector: 'app-navbar',
  templateUrl: './navbar.component.html',
  styleUrls: ['./navbar.component.scss']
})
export class NavbarComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

navbar.component.html

<p>nav bar is working</p>

index.html

<app-navbar></app-navbar>

but when I run localhost server it is not displaying nav bar is working in my index.html. how can I fix this?

You should use your component in app.component.html page like this -

<app-navbar></app-navbar>

Not in index.html because this is not your bootstrap component.

you always use <app-root></app-root> component in index.html file because this is the component which bootstraped in main module like this -

bootstrap: [AppComponent]

app-module.ts导入导航栏, app-module.ts <app-navbar></app-navbar>放入app.component.html

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