typescript Angular2 设置默认路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42155766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:15:12  来源:igfitidea点击:

Angular2 setting default route

angulartypescript

提问by Android coder

Am setting my routing in the module and i would like to set default route but it fails

我正在模块中设置我的路由,我想设置默认路由但它失败了

This is the routing module

这是路由模块

const appRoutes: Routes = [
 { path: 'login', component: LoginComponent, useAsDefault:true }, //returns an error
 {
  path: 'dash',
  redirectTo:"dashboard"
},

{ path: 'reset-password', component: ResetPasswordComponent },
{ path: '',    redirectTo: '/dashboard', pathMatch: 'full'  },
{ path: '**', component: PageNotFoundComponent }
];

The above returns an error of

以上返回错误

LoginComponent; useAsD...' is not assignable to type 'Route[]'

What could be wrong

什么可能是错的

回答by ER.SHASHI TIWARI

When using useAsDefault you need to have the parent route and the useAsDefault on the child route you want to appear first. So, No need of useAsDefault, You can simply gives Login As Default Routes.And As I see there is no Imported component for Dashboard so,

使用 useAsDefault 时,您需要在要首先出现的子路由上拥有父路由和 useAsDefault。所以,不需要 useAsDefault,您可以简单地将登录作为默认路由。而且我看到仪表板没有导入组件,所以,

const appRoutes: Routes = [
  {
    path: '',
    redirectTo: "/login",
    pathMatch: 'full'
  },
  { path: 'login', component: LoginComponent },
  { path: 'reset-password', component: ResetPasswordComponent },
  { path: '**', component: PageNotFoundComponent }
];