Angular 路由器在路由器变量中处理“/”
Angular Router handling "/" in a router variable
我一直在使用 routing 进行 Angular 项目,所以让我们考虑下面这样的路线。
{
path : 'preview/:Id',
loadChildren : () => import('./path/preview/preview.module').then( m => m.PreviewModule),
}
所以这个 route
可能有像 (a-zA-Z0-9) 这样的字母数字值,也有像 /
这样的特殊字符,由 crypto.js 产生的哈希值如下所示。
preview/QQZnNw+VjBg/cAXvy6nxdiQ==
所以在上面的这条路线中,参数 Id
会有这样的值 QQZnNw+VjBg/cAXvy6nxdiQ==
,这就是我想要达到的目标。
Error : But unfortunately am getting error stating unrecognised route because , this above value has "/" in its route.
之前我尝试过的是,我尝试将这样的正则表达式 /:Id(/[\S]+/g)
添加到路由参数中,以便它可以接受这条路由,
{
path : 'preview/:Id(/[\S]+/g)',
loadChildren : () => import('./path/preview/preview.module').then( m => m.PreviewModule),
}
拜托,谁能帮我想个办法。
您可以使用 UrlMatcher
.
function slashMatcher(path: string, param: string): UrlMatcher {
return (
segments: UrlSegment[],
group: UrlSegmentGroup,
route: Route
): UrlMatchResult => {
const { length } = segments;
const firstSegment = segments[0];
if (firstSegment.path === path && length >= 2) { // firstSegment.path == preview
const paramSegments = segments.slice(1);
const paramPaths = paramSegments.map(segment => segment.path); // ["QQZnNw+VjBg", "cAXvy6nxdiQ=="]
const mergedParam = paramPaths.join("/"); // QQZnNw+VjBg/cAXvy6nxdiQ==
const idSegment: UrlSegment = new UrlSegment(mergedParam, { [param]: mergedParam });
return { consumed: segments, posParams: { [param]: idSegment } };
}
return null;
};
}
const routes: Routes = [
{
matcher: slashMatcher('preview', 'Id'),
loadChildren : () => import('./path/preview/preview.module').then( m => m.PreviewModule),
},
];
// In component
const id$ = this.route.paramMap.pipe(map(params => params.get('Id')));
多亏了这个,我找到了解决方法。
I have created two utility functions to replace "/" with some hashcode when encrypting and while decrypting that hashcode back to "/".
convertSpecial = (text: string) => {
const ciphertext = this.encrypt(text);
return ciphertext.replace(/\+/g,'p1L2u3S').replace(/\//g,'s1L2a3S4h').replace(/=/g,'e1Q2u3A4l');
}
unconvertSpecial = (ciphertext: string) => {
ciphertext = ciphertext.toString().replace(/p1L2u3S/g, '+' ).replace(/s1L2a3S4h/g, '/').replace(/e1Q2u3A4l/g, '=');
const text = this.decrypt(ciphertext);
return text;
}
所以在重定向时,
<a href="#" [routerLink]="['/preview', convertSpecial(item?._id)]" > </a>
关于安装组件,
ngOnit()
{
this.Id = this.unconvertSpecial(this.Id);
}
我一直在使用 routing 进行 Angular 项目,所以让我们考虑下面这样的路线。
{
path : 'preview/:Id',
loadChildren : () => import('./path/preview/preview.module').then( m => m.PreviewModule),
}
所以这个 route
可能有像 (a-zA-Z0-9) 这样的字母数字值,也有像 /
这样的特殊字符,由 crypto.js 产生的哈希值如下所示。
preview/QQZnNw+VjBg/cAXvy6nxdiQ==
所以在上面的这条路线中,参数 Id
会有这样的值 QQZnNw+VjBg/cAXvy6nxdiQ==
,这就是我想要达到的目标。
Error : But unfortunately am getting error stating unrecognised route because , this above value has "/" in its route.
之前我尝试过的是,我尝试将这样的正则表达式 /:Id(/[\S]+/g)
添加到路由参数中,以便它可以接受这条路由,
{
path : 'preview/:Id(/[\S]+/g)',
loadChildren : () => import('./path/preview/preview.module').then( m => m.PreviewModule),
}
拜托,谁能帮我想个办法。
您可以使用 UrlMatcher
.
function slashMatcher(path: string, param: string): UrlMatcher {
return (
segments: UrlSegment[],
group: UrlSegmentGroup,
route: Route
): UrlMatchResult => {
const { length } = segments;
const firstSegment = segments[0];
if (firstSegment.path === path && length >= 2) { // firstSegment.path == preview
const paramSegments = segments.slice(1);
const paramPaths = paramSegments.map(segment => segment.path); // ["QQZnNw+VjBg", "cAXvy6nxdiQ=="]
const mergedParam = paramPaths.join("/"); // QQZnNw+VjBg/cAXvy6nxdiQ==
const idSegment: UrlSegment = new UrlSegment(mergedParam, { [param]: mergedParam });
return { consumed: segments, posParams: { [param]: idSegment } };
}
return null;
};
}
const routes: Routes = [
{
matcher: slashMatcher('preview', 'Id'),
loadChildren : () => import('./path/preview/preview.module').then( m => m.PreviewModule),
},
];
// In component
const id$ = this.route.paramMap.pipe(map(params => params.get('Id')));
多亏了这个
I have created two utility functions to replace "/" with some hashcode when encrypting and while decrypting that hashcode back to "/".
convertSpecial = (text: string) => {
const ciphertext = this.encrypt(text);
return ciphertext.replace(/\+/g,'p1L2u3S').replace(/\//g,'s1L2a3S4h').replace(/=/g,'e1Q2u3A4l');
}
unconvertSpecial = (ciphertext: string) => {
ciphertext = ciphertext.toString().replace(/p1L2u3S/g, '+' ).replace(/s1L2a3S4h/g, '/').replace(/e1Q2u3A4l/g, '=');
const text = this.decrypt(ciphertext);
return text;
}
所以在重定向时,
<a href="#" [routerLink]="['/preview', convertSpecial(item?._id)]" > </a>
关于安装组件,
ngOnit()
{
this.Id = this.unconvertSpecial(this.Id);
}