Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度:替换url中的段_Angular_Angular Routing_Angular Router - Fatal编程技术网

Angular 角度:替换url中的段

Angular 角度:替换url中的段,angular,angular-routing,angular-router,Angular,Angular Routing,Angular Router,此问题与以下现有问题重复,因此: 这些问题的答案都不能完全解决替换url中一个段的问题,因此我再次问这个问题 总结我的情况: 这些是我项目中的一些路线(我自己没有想到这一点,所以请容忍我) 参数“:customer”出现在url的各个位置。我的问题是,此客户id可能在后端更改,而不需要前端执行任何操作。我正在收听后端的更改,并且必须相应地调整url以防客户id更改 简单的string.replace不行,因为从技术上讲,客户id实际上可能是“静态”或“其他”的 问题: 如何分析当前ur

此问题与以下现有问题重复,因此:

这些问题的答案都不能完全解决替换url中一个段的问题,因此我再次问这个问题

总结我的情况:

这些是我项目中的一些路线(我自己没有想到这一点,所以请容忍我)

参数“:customer”出现在url的各个位置。我的问题是,此客户id可能在后端更改,而不需要前端执行任何操作。我正在收听后端的更改,并且必须相应地调整url以防客户id更改

简单的string.replace不行,因为从技术上讲,客户id实际上可能是“静态”或“其他”的

问题:


如何分析当前url(ActivatedRoute?)以仅替换url中与客户id有关的一部分(paramMap有“customer”),同时保持url的其余部分不变?

要读取字符串中的文本模式,我建议使用正则表达式

例如,如果您的客户ID类似于“123456”,则正则表达式可以是/\d{6}/(这里的斜线是用来描述正则表达式模式的,而不是url模式):

注意定义一个仅与URL中的客户id匹配的模式

您可以在此处找到有关JavaScript正则表达式的更多信息:

我差点忘了,您可以使用string.replace()函数替换字符串中的模式:
我确实写了自己的生成器。代码如下:

从“@angular/router”导入{ActivatedRoute}”;
const defaultExtras:{
帕拉姆斯:任何,
矩阵:布尔,
递归:布尔,
矩阵:布尔,
replaceChildParams:布尔值
} = {
参数:{},
是的,
是的,
矩阵:false,
replaceChildParams:true
};
导出函数routerPathGenerator(
路线:激活的路线,
附加?:{params?:any,preserveMatrix?:布尔,递归?:布尔,preserveChildMatrix?:布尔,replaceChildParams?:布尔}
):任何[]{
const mergedExtras={…defaultExtras,…extras};
const path:any[]=[…replaceRouteParams(route.routeConfig.path.split('/'),{…route.snapshot.params,…mergedExtras.params}];
if(mergedExtras.preserveMatrix){
push(replaceMartixParams(route,mergedExtras.params));
}
if(mergedExtras.recursive&&route.children.length>0){
path.push(…routerPathGenerator(route.children[0]{
params:mergedExtras.replaceChildParams?mergedExtras.params:{},
preserveMatrix:mergedExtras.preserveChildMatrix,
递归:mergedExtras.recursive,
preserveChildMatrix:mergedExtras.preserveChildMatrix,
replaceChildParams:mergedExtras.replaceChildParams
}));
}
返回路径;
}
函数replaceRouteParams(部分:字符串[],参数:对象):任意[]{
退货(零件| |[])
.map(part=>(part.startsWith(“:”)和¶ms.hasOwnProperty(part.substring(1))?params[part.substring(1)]:part));
}
函数replaceMatrixParams(路由:ActivatedRoute,参数:Object):任意{
常数矩阵:any={};
Object.key(route.snapshot.params)
.forEach(键=>
(route.routeConfig.path.split('/')。一些(p=>p===':'+键)?
'' : 
矩阵[key]=params.hasOwnProperty(key)?params[key]:路由.snapshot.params[key])
)
收益矩阵;
}
假设我们有如下URL:

http://some.domain/warehouses/5;c=7;d=8/moves/42/items;a=1;b=2?e=77&f=88
其中“/warehouses”指向具有路由的延迟加载模块:

const routes:routes=[
{路径:'',组件:WarehousesComponent},
{路径:':id',重定向到:':id/stocks',路径匹配:'full'},
{路径:':id',组件:仓库组件,子级:[
{路径:'stocks',组件:StocksComponent},
{路径:“移动”,组件:移动组件,子级:[
{路径:'',重定向到:'items',路径匹配:'full'},
{path:':moveId/items',component:ItemsComponent},
{path:'initial',component:InitialComponent}
] }
] }
];
如果我在WarehousesComponent中运行下一个代码

const commands=routerPathGenerator(this.route{
参数:{
id:8,
移动ID:24,
c:17,
a:666
},
这是真的吗
});
this.router.navigate(命令,{queryParamsHandling:'preserve',relativeTo:this.route.parent});
我将导航到

http://some.domain/warehouses/8;c=17;d=8/moves/24/items;a=666;b=2?e=77&f=88

我想这是你的情况,只需查看routerPathGenerator的附加参数即可

谢谢你的回答!我尽量不使用String.replace,因为理论上,客户id可能是任何东西,包括“某物”…如果你不知道你的:客户在url中的位置,也不会是什么样子,祝你好运:)你没有其他信息吗?@Abel,有一个路由器配置,指示angular router service如何解析到组件的路径。主要问题是如何使用此配置中的“元数据”替换url段。您找到解决方案了吗?遗憾的是,没有。。。。。。。。
http://some.domain/warehouses/5;c=7;d=8/moves/42/items;a=1;b=2?e=77&f=88
http://some.domain/warehouses/8;c=17;d=8/moves/24/items;a=666;b=2?e=77&f=88