javascriptangulartypescripterror-handlingroutes

Angular 2 RC5 and above - How to show an error page when no route matches?


My current Route config looks like this

{
    path: '',
    redirectTo: 'register/account',
    pathMatch: 'full'
},
{
    path: 'register/account',
    component: AccountRegisterComponent
},
 {
    path: 'register/auto/:Id',
    component:AutoregisterComponent
},

If a user tries to navigate to '/register/auto'. Currently I see a blank page and an error in the console. How can I show a 404 error or a message saying that this page is not available globally?


Solution

  • See angular2 cheatsheet You can do something like this:

    {path: '**', redirectTo: '/404' }
    

    So for example you would have something probably like this:

    {path: '/404', component: NotFoundComponent},
    {path: '**', redirectTo: '/404' }
    

    Hope that helps