Eduardo Spaki
Eduardo Spaki

Reputation: 1188

default error pages with angular2

I'd like to have a default error page my application. to achieve that, I implemented an error handler:

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

and create error-page component and route as well:

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

but this line, at error hadler does not work... someone have some idea about why?

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

I mean, I'm receiving the error but I'm not able to route to my error page!

Upvotes: 0

Views: 2368

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657068

Routes with an empty path '' and without child routes need pathMatch: 'full'

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

Also a leading / ensures the navigation works in all cases

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

Upvotes: 1

Related Questions