Let's create a new project:
Then let's create two components: intro and body:
Let's update the template app.component.html:
And the module file app.module.ts
Done! Now we can switch between the two components:
Then let's create two components: intro and body:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cd routing | |
ng generate component intro | |
ng generate component body | |
npm start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Switching between pages</h1> | |
<a routerLink="intro">Intro</a> <!-- The link to the intro component !--> | |
<a routerLink="body">Body</a> <!-- The link to the body component !--> | |
<router-outlet></router-outlet> <!-- The place where to display the component !--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { BrowserModule } from '@angular/platform-browser'; | |
import { NgModule } from '@angular/core'; | |
import { AppComponent } from './app.component'; | |
import { IntroComponent } from './intro/intro.component'; | |
import { BodyComponent } from './body/body.component'; | |
import { RouterModule } from "@angular/router"; | |
@NgModule({ | |
declarations: [ | |
AppComponent, | |
IntroComponent, | |
BodyComponent | |
], | |
imports: [ | |
BrowserModule, | |
RouterModule.forRoot([ | |
{path: 'intro', component: IntroComponent}, | |
{path: 'body', component: BodyComponent}, | |
{path: '', redirectTo: '/intro', pathMatch: 'full'} | |
]) | |
], | |
providers: [], | |
bootstrap: [AppComponent] | |
}) | |
export class AppModule { } |
No comments:
Post a Comment