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

六月ゝ 毕业季﹏ 提交于 2019-12-22 08:46:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!