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

Angular路由定义多可选参数时出现异常重定向问题如何解决?

问题根因

你配置的explorer下的空路径重定向规则没有显式指定pathMatch: 'full',Angular路由默认的匹配策略为prefix,即只要当前路径的前缀匹配规则就会触发对应逻辑。
当你访问/explorer/camera时,空字符串作为任何路径的前缀都会匹配到空路径规则,因此会触发重定向,在当前路径后拼接camera,最终得到异常的/explorer/camera/camera路径。

修正后的路由配置
{
  path: "explorer",
  children: [
    {
      path: "",
      redirectTo: "camera",
      pathMatch: "full" // 仅完全匹配空路径时才触发重定向
    },
    {
      path: ":itemType/:itemId",
      pathMatch: "full",
      component: ExplorerComponent
    },
    {
      path: ":itemType",
      pathMatch: "full",
      component: ExplorerComponent
    }
  ]
}
效果验证
  • 访问/explorer:完全匹配空路径规则,正常重定向到/explorer/camera
  • 访问/explorer/camera:不会触发空路径重定向,匹配:itemType规则,正常加载ExplorerComponent
  • 访问/explorer/camera/1:匹配双参数规则,运行逻辑不变

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 07:15:03