Skip to content

Instantly share code, notes, and snippets.

@killerchip
Last active July 29, 2018 08:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save killerchip/79d5df784cca703f21f81c8294ddf2b6 to your computer and use it in GitHub Desktop.
Save killerchip/79d5df784cca703f21f81c8294ddf2b6 to your computer and use it in GitHub Desktop.
Angular cheatsheet - Basic routing

Angular Basic Routing

The following is an example on how to setup basic routing

import RouterModule and Routes

app.module.ts:

import { RouterModule, Routes } from '@angular/router';

define your routes

app.module.ts:

const routes: Routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full' },
  { path: 'home', component: HomeComponent },
  { path: 'about', component: AboutComponent }
];

import the Module

app.module.ts:

@NgModule: ({
  ...
  imports: [
    ...
    RouterModule.forRoot(routes)
    ]
  ...
})

use router-outlet to place content

<router-outlet> component places the contents of the route [routerLink] allows to navigate to path without reloading the page.

app.component.html:

<div class="navbar">
  <a [routerLink]=['/home']> [HOME] </a>
  <a [routerLink]=['/about']> [ABOUT] </a>
</div>

<!-- router content goes here -->
<router-outlet></router-outlet>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment