Angular 主路由的角度无效配置(空路径)
在我的路由器配置中,我在路径Angular 主路由的角度无效配置(空路径),angular,redirect,routing,angular-routing,angular-router,Angular,Redirect,Routing,Angular Routing,Angular Router,在我的路由器配置中,我在路径“dashboard”下配置了一个仪表板组件,我想在没有指定路径时自动将用户重定向到此路由(空路径,所以只需/) 这是我的代码: const appRoutes: Routes = [ { path: '', redirectTo: 'dashboard'}, { path: 'dashboard', component: DashboardComponent }, /* other routes... */ ]; 未处理的承诺拒绝:路由{path: “
“dashboard”
下配置了一个仪表板组件
,我想在没有指定路径时自动将用户重定向到此路由(空路径,所以只需/
)
这是我的代码:
const appRoutes: Routes = [
{ path: '', redirectTo: 'dashboard'},
{ path: 'dashboard', component: DashboardComponent },
/* other routes... */
];
未处理的承诺拒绝:路由{path:
“”,重定向到:“仪表板”}”:请提供“路径匹配”。默认值
“pathMatch”的值是“prefix”,但其目的通常是使用
“满”
问题是缺少空路由的
pathMatch
属性,该属性默认为prefix
但是在这种情况下,路径匹配
值应设置为完整
:
const appRoutes: Routes = [
{ path: '', redirectTo: 'dashboard', pathMatch: 'full'},
{ path: 'dashboard', component: DashboardComponent },
/* other routes... */
];
原因如下:
从技术上讲,当
URL的其余未匹配段与“”匹配。在这个例子中,
重定向位于顶级路由中,因此剩余的URL和
整个URL都是一样的
另一个可能的路径匹配值是“prefix”,它告诉路由器
当剩余URL以开头时匹配重定向路由
重定向路由的前缀路径
不要在这里这样做。如果路径匹配值为“前缀”,则每个URL
将匹配“”
有关更多详细信息,请参阅官方文档:@AndreiMatracaru Stackoverflow允许我在2天后接受自己的答案