pass static data from route in Angular 4
Create a error page component
ng g c error page
–error-page.component.html
<h4>{{ errorMassage }}</h4>
–error-page.component.ts
import { Component, OnInit } from '@angular/core'; import { ActivatedRoute, Data } from '@angular/router'; @Component({ selector: 'app-error-page', templateUrl: './error-page.component.html', styleUrls: ['./error-page.component.css'] }) export class ErrorPageComponent implements OnInit { errorMassage: string; constructor(private route: ActivatedRoute) { } ngOnInit() { this.route.data.subscribe( (data: Data) => { this.errorMassage = data['message']; } ) } }
–app-routing.module.ts
const appRoutes: Routes = [ { path: '', redirectTo:'/home', pathMatch:'full'}, { path: 'home', component: HomeComponent}, { path: 'users', component: UsersComponent, children: [ { path: ':id/:name', component: UserComponent}, ]}, { path: 'servers', canActivateChild:[AuthGuard], component: ServersComponent, children: [ { path: ':id', component: ServerComponent}, { path: ':id/edit', component: EditServerComponent, canDeactivate: [CanDeactivateGuard]}, ]}, { path: 'error-404', component: ErrorPageComponent, data: {message: 'Page not found!'}}, { path: '**', redirectTo: '/error-404'} ]