具有angular2的默认错误页面

时间:2017-04-02 13:56:27

标签: angular

我希望我的应用程序有一个默认错误页面。为实现这一点,我实现了一个错误处理程序:

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 }
];

但这行,错误的hadler不起作用......有人对某些原因有所了解吗?

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

我的意思是,我收到错误但我无法路由到错误页面!

1 个答案:

答案 0 :(得分:1)

路径为空''且无子路线的路线需要pathMatch: 'full'

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

同样领先的/可确保导航在所有情况下都能正常工作

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