How to redirect a nonexistent link to the main page in Angular 2?

If the user enters a link that does not exist, I want the page to be redirected to the main page.

How can i do this? Thanks

@RouteConfig([
    {path: '/home', name: 'Home', component: HomeComponent},
    {path: '/about', name: 'About', component: AboutComponent},
    {path: '/???', name: 'not-found', redirectTo: ['Home']}
])
+4
source share
2 answers

This will redirect all unregistered routes to Home.

{ path: "/**", redirectTo: ["Home"] }
+6
source

In Routing v4, the property name no longer exists. the route defines without the name property. so you should use the path, not the name redirectTo: '/ redirectPath' and no leading slash for the path, so use the path: '404' instead of the path: '/ 404'

as:

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

Source: https://habr.com/ru/post/1629723/


All Articles