2017-04-02 34 views
0

我想有一個默認的錯誤頁面我的應用程序。實現這一目標,我實現了一個錯誤處理程序:默認錯誤頁面與angular2

import { ErrorHandler, Injectable, Injector } from '@angular/core'; 
import { Router } from '@angular/router'; 

@Injectable() 
export class GlobalErrorHandler implements ErrorHandler { 
    router: Router; 

    constructor(private injector: Injector) { 
    this.router = this.injector.get(Router); 
    } 

    handleError(error) { 
    this.router.navigate(['error']); 
    throw error; 
    } 
} 

,並創建錯誤頁面組件和路線,以及:

export const routes: Routes = [ 
    { path: '', component: RecipeListComponent }, 
    ... 
    { path: 'error', component: ErrorPageComponent } 
]; 

,但此行,在錯誤哈德勒不工作...有人有一些想法爲什麼?

this.router.navigate(['error']); 

我的意思是,我收到了錯誤信息,但我無法路由到我的錯誤頁面!

回答

1

路由與空路徑'',無子路由需要pathMatch: 'full'

{ path: '', component: RecipeListComponent, pathMatch: 'full' }, 

而且領先/確保導航在所有情況下工作

this.router.navigate(['/error']);