如何处理带有双斜杠的url?

问题描述 投票:0回答:1

出于遗留原因,我需要处理一条可能存在两个斜杠的路线,因为该路径中存在一个可选的ID(例如/ base // child / 2)。无论如何,我无法更改url(例如,省略第二个斜杠以使用辅助路径)。无论如何,我可以让角度路由器识别此网址并重定向或处理它吗?

minimal stackblitz example显示了问题。

angularjs angular angular-router
1个回答
0
投票

我会沿着这些路线尝试UrlMatcher一些东西

matcher.ts

import { UrlSegment } from '@angular/router';

export function matcher(url:  UrlSegment[]) {
  console.log(JSON.stringify(url));
 return url.length === 1 && url[0].path === 'base' ? ({consumed: url}) : null;
}

路线

import  { matcher } from './matcher';

RouterModule.forRoot([{
  matcher: matcher,
  component: InformedComponent
}, {
  path: 'base/:parentId/child/:id',
  component: InformedComponent
}, {
  path: '**',
  component: CatchAllComponent
}])
]

与您的stacknblitz进行了尝试,它可以正常工作。不确定,但其他选项可能正在侦听路由器的导航启动事件或路由防护。

© www.soinside.com 2019 - 2024. All rights reserved.