问题
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']}
])
回答1:
This will redirect all unregistered routes to Home
{ path: "/**", redirectTo: ["Home"] }
回答2:
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'}
来源:https://stackoverflow.com/questions/35529550/how-to-redirect-not-existing-link-to-home-page-in-angular-2