在Angular中有没有办法在URL中不带参数的情况下提取路径?
假设我有以下网址:http://myapp.com/1005/Customers
其中1005
是参数,但我只需要参数后面的段,在本例中是Customers
。
现在我使用split
来做这件事。我可以用一种更优雅的方式来实现吗?
发布于 2017-08-22 12:44:27
您可以使用ActivatedRoute
。您只需将其导入到您的构造函数中:private route: ActivatedRoute
。
然后您就可以订阅params了:
this.route.params.subscribe((params: Params){
//Access your param or iterate over the keys.
this.params['Customers']
})
发布于 2019-03-25 13:32:32
我找到了一种从路由配置中构建相对url的方法:
let path = '';
let currentRoute = this.route.snapshot;
while (currentRoute) {
if (currentRoute.routeConfig) {
path += currentRoute.routeConfig.path + '/';
}
if (currentRoute.children && currentRoute.children.length > 0) {
currentRoute = currentRoute.children[0];
} else {
currentRoute = undefined;
}
}
此代码将为您提供一种获得类似/part/subpart/:param/ obtain part的内容的方法
https://stackoverflow.com/questions/45817828
复制相似问题