Router enables navigation from one view to another based on user interactions with the application.
Following are the steps in implementing basic routing in Angular -
NOTE: Ensure you have this tag:
<base href='/'>
as the first child under your head tag in your index.html file. This element states that your app folder is the application root. Angular would then know how to organize your links.
Check if you are pointing to the correct/latest routing dependencies in package.json (using the latest version of Angular) and that you already did an npm install
-
"dependencies": {
"@angular/router": "^4.2.5"
}
Define the route as per its interface definition:
interface Route {
path?: string;
pathMatch?: string;
component?: Type<any>;
}
In a routing file (routes/app.routing.ts
), import all the components which you need to configure for different routing paths. Empty path means that view is loaded by default. ":" in the path indicates dynamic parameter passed to the loaded component.
import { Routes, RouterModule } from '@angular/router';
import { ModuleWithProviders } from '@angular/core';
import { BarDetailComponent } from '../components/bar-detail.component';
import { DashboardComponent } from '../components/dashboard.component';
import { LoginComponent } from '../components/login.component';
import { SignupComponent } from '../components/signup.component';
export const APP_ROUTES: Routes = [
{ path: '', pathMatch: 'full', redirectTo: 'login' },
{ path: 'dashboard', component: DashboardComponent },
{ path: 'bars/:id', component: BarDetailComponent },
{ path: 'login', component: LoginComponent },
{ path: 'signup', component: SignupComponent }
];
export const APP_ROUTING: ModuleWithProviders = RouterModule.forRoot(APP_ROUTES);
In your app.module.ts
, place this under @NgModule([])
under imports
:
// Alternatively, just import 'APP_ROUTES
import {APP_ROUTING} from '../routes/app.routing.ts';
@NgModule([
imports: [
APP_ROUTING
// Or RouterModule.forRoot(APP_ROUTES)
]
])
Load/display the router components based on path accessed. The <router-outlet>
directive is used to tell angular where to load the component.
import { Component } from '@angular/core';
@Component({
selector: 'demo-app',
template: `
<div>
<router-outlet></router-outlet>
</div>
`
})
export class AppComponent {}
Link the other routes. By default, RouterOutlet
will load the component for which empty path is specified in the Routes
. RouterLink
directive is used with html anchor tag to load the components attached to routes. RouterLink
generates the href attribute which is used to generate links. For example:
import { Component } from '@angular/core';
@Component({
selector: 'demo-app',
template: `
<a [routerLink]="['/login']">Login</a>
<a [routerLink]="['/signup']">Signup</a>
<a [routerLink]="['/dashboard']">Dashboard</a>
<div>
<router-outlet></router-outlet>
</div>
`
})
export class AnotherComponent { }
Now, we are good with routing to static paths. RouterLink
supports dynamic path too by passing extra parameters along with the path.
import { Component } from '@angular/core';
@Component({
selector: 'demo-app',
template: `
<ul>
<li *ngFor="let bar of bars | async">
<a [routerLink]="['/bars', bar.id]">
{{bar.name}}
</a>
</li>
</ul>
<div>
<router-outlet></router-outlet>
</div>
`
})
export class SecondComponent { }
RouterLink
takes an array where the first parameter is the path for routing and subsequent elements are for the dynamic routing parameters.