error-page.component.html
<h4>{{errorMessage}}</h4>
app.module.ts
import {BrowserModule} from '@angular/platform-browser'; import {NgModule} from '@angular/core'; import {FormsModule} from '@angular/forms'; import {HttpModule} from '@angular/http'; import {AppComponent} from './app.component'; import {HomeComponent} from './home/home.component'; import {UsersComponent} from './users/users.component'; import {ServersComponent} from './servers/servers.component'; import {UserComponent} from './users/user/user.component'; import {EditServerComponent} from './servers/edit-server/edit-server.component'; import {ServerComponent} from './servers/server/server.component'; import {ServersService} from './servers/servers.service'; import {RouterModule, Routes} from '@angular/router'; import {PageNotFoundComponent} from './page-not-found/page-not-found.component'; import {AuthService} from './auth.service'; import {AuthGuard} from './auth.guard'; import {CanDeactivateGuard} from './can-deactivate.guard'; import {ErrorPageComponent} from './error-page/error-page.component'; const appRoutes: Routes = [ {path: '', 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: 'not-found', component: PageNotFoundComponent}, {path: 'not-found', component: ErrorPageComponent, data: {message: 'Page not found'}}, {path: '**', redirectTo: '/not-found'}, ]; @NgModule({ declarations: [ AppComponent, HomeComponent, UsersComponent, ServersComponent, UserComponent, EditServerComponent, ServerComponent, PageNotFoundComponent, ErrorPageComponent ], imports: [ BrowserModule, FormsModule, HttpModule, RouterModule.forRoot(appRoutes) ], providers: [ServersService, AuthGuard, AuthService, CanDeactivateGuard], bootstrap: [AppComponent] }) export class AppModule { }
error-page.component.ts
import {Component, OnInit} from '@angular/core'; import {ActivatedRoute} from '@angular/router'; @Component({ selector: 'app-error-page', templateUrl: './error-page.component.html', styleUrls: ['./error-page.component.css'] }) export class ErrorPageComponent implements OnInit { errorMessage: string; constructor(private route: ActivatedRoute) { } ngOnInit() { this.errorMessage = this.route.snapshot.data['message']; this.route.data.subscribe((value: Date) => { this.errorMessage = value['message']; }); } }