Hongbo Miao
Hongbo Miao

Reputation: 49704

How to redirect not-existing link to Home page in Angular 2?

If user input a not-existing link, I want the page redirect to Home page.

How can I do it? Thanks

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

Upvotes: 4

Views: 2226

Answers (2)

CREM
CREM

Reputation: 1991

In Routing of the version v4 name property no more exist. route define without name property. so you should use path instead of name redirectTo: '/redirectPath' and no leading slash for path so use path: '404' instead of path: '/404'

like:

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

Upvotes: 1

Dan Rasmuson
Dan Rasmuson

Reputation: 6023

This will redirect all unregistered routes to Home

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

Upvotes: 7

Related Questions