I follow the Heroes Tutorial of Angular to learn it. However, after I create the project, generate the component, modify the HTML, nothing displays on the page, not as expected. Can someone solve it? Thanks a lot!
Here are my codes
heros.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-heros',
templateUrl: './heros.component.html',
styleUrls: ['./heros.component.css']
})
export class HerosComponent implements OnInit {
hero="WindStorm"
constructor() { }
ngOnInit(): void {
}
}
heros.component.html
<h2>{{hero}}</h2>
app.component.html
<h1>
{{title}}
</h1>
<app-heros></app-heros>
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HerosComponent } from './heros/heros.component';
@NgModule({
declarations: [
AppComponent,
HerosComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app-routing.modules.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import {HerosComponent} from './heros/heros.component'
const routes: Routes = [{path:'', component:HerosComponent}];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
I think the issue is the way you are routing. Your app.component.html should look something like this
<h1>
{{title}}
</h1>
<router-outlet></router-outlet>
The following line basically replaces the router-outlet tag with an instance of the Heroes Component:
const routes: Routes = [{path:'', component:HerosComponent}];
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.