Angular 带参数的路由,带参数的重定向路由与不带参数的路由分开

Angular routing with params, redirect route with params separately from one without params

在 Angular (v7) 应用程序中,我有这样的路由设置:

export const routes: Routes = [
  {
    path: '', 
    component: HomeComponent,
    children: [{
      path: '',
      component: Main
    },
    {
      path: 'main',
      component: Main
    },
     {
      path: 'search/:imageid',
      component: Image
    }, 
    {
      path: 'search',
      component: Search
    }]
}]

我期望发生的事情是这样的 URL:

domain.com/#/search;imageid=512

路由器将重定向到图像组件,对于这样的 URL:

domain.com/#/search

路由器将重定向到搜索组件,但搜索正在获取这两个组件,并且绕过了到图像的路由。

我不确定我在这里遗漏了什么。

您无需重复路径变量的名称。

您应该使用的路径是 domain.com/#/search/512,路由器将按预期处理变量。

(确保您正确定义了 HashLocationStrategy)。

此外,知道路由上 pathMatch: 'full' 属性 的存在也很有用,它允许匹配整个路径而不仅仅是前缀。这可能是这里的问题,但这不是因为您放置子路由的顺序。查看 this place in the official docs 以获得更精确的开发。