- Create an angular project, when asked for Angular routing support, say "YES"
- Create routes in app.module.ts
- Write the [routerLinks] in app.component.html
- Writer the in app.component.html
- Make sure there is a line in the head section of app.component.html
Created
December 19, 2018 01:13
-
-
Save tedhagos/2b91b25ef7a91b4fb6e096538372d9c9 to your computer and use it in GitHub Desktop.
Angular Routing
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>{{ title }}</h1> | |
<ul> | |
<li> | |
<a [routerLink]="['/one']">One</a> | |
</li> | |
<li> | |
<a [routerLink]="['/two']">Two</a> | |
</li> | |
</ul> | |
<router-outlet></router-outlet> |
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 { AppRoutingModule } from './app-routing.module'; | |
import { AppComponent } from './app.component'; | |
import { RouterModule } from '@angular/router'; | |
import { OneComponent } from './one/one.component'; | |
import { TwoComponent } from './two/two.component'; | |
import { WelcomeComponent } from './welcome/welcome.component'; | |
@NgModule({ | |
declarations: [ | |
AppComponent, | |
OneComponent, | |
TwoComponent, | |
WelcomeComponent | |
], | |
imports: [ | |
BrowserModule, | |
AppRoutingModule, | |
RouterModule.forRoot([ | |
{path: 'one', component: OneComponent }, | |
{path: 'two', component: TwoComponent }, | |
{path: 'welcome', component: WelcomeComponent }, | |
{path: '', redirectTo: 'welcome', pathMatch: 'full'} | |
], {useHash: true}) | |
], | |
providers: [], | |
bootstrap: [AppComponent] | |
}) | |
export class AppModule { } |
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 { Component, OnInit } from '@angular/core'; | |
@Component({ | |
templateUrl: './one.component.html', | |
styleUrls: ['./one.component.css'] | |
}) | |
export class OneComponent implements OnInit { | |
title = "This is one component" | |
constructor() { } | |
ngOnInit() { | |
} | |
} |
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 { Component, OnInit } from '@angular/core'; | |
@Component({ | |
templateUrl: './two.component.html', | |
styleUrls: ['./two.component.css'] | |
}) | |
export class TwoComponent implements OnInit { | |
title = "Two Component works" | |
constructor() { } | |
ngOnInit() { | |
} | |
} |
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 { Component, OnInit } from '@angular/core'; | |
@Component({ | |
templateUrl: './welcome.component.html', | |
styleUrls: ['./welcome.component.css'] | |
}) | |
export class WelcomeComponent implements OnInit { | |
title = "Welcome"; | |
constructor() { } | |
ngOnInit() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment