You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular路由重定向异常求助:访问/admin路径未跳转至/admin/login而是跳转到根路径

解决Angular Admin路由跳转异常问题

我帮你排查了下,问题出在AdminModule子路由的redirectTo配置写法上。

你当前在AdminModule的子路由里写了:

{path: '', redirectTo: '/admin/login', pathMatch: 'full'},

这里用了带开头斜杠的绝对路径,但在Angular子路由的上下文里,这种写法会让路由导航忽略当前父路由的/admin前缀,反而触发根路由的匹配规则,最终意外跳转到了localhost:4200。

修复方案

把redirectTo改成相对路径,去掉开头的斜杠和admin前缀:

{path: '', redirectTo: 'login', pathMatch: 'full'},

这样当你访问localhost:4200/admin时,Angular会自动基于父路由/admin拼接相对路径login,正确跳转到localhost:4200/admin/login。

额外小提示

你AppRouting里的根路由配置还有个潜在问题:

children: [
  {path: '', redirectTo: '/', pathMatch: 'full'},
  {path: '', component: MainPageComponent},
]

两个path: ''的路由会造成无限循环隐患(访问根路径时不断重定向到自己),建议简化成:

children: [
  {path: '', component: MainPageComponent},
  {path: 'product/:id', component: ProductPageComponent},
  {path: 'cart', component: CartPageComponent}
]

如果需要默认重定向到某个具体路径(比如home),再单独设置redirectTo规则即可。

内容的提问来源于stack exchange,提问作者alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 00:42:35