[英]path url split() find() angular
I have urls and I want to recover the beginning of the path but I don't know how to do it and being a beginner according to my research I should use the split() and find() method.我有 url,我想恢复路径的开头,但我不知道该怎么做,根据我的研究,作为初学者,我应该使用 split() 和 find() 方法。
Here are the urls:以下是网址:
/test/someurls /test/someurls
/angular/someurls /角/someurls
I need to get /test and /angular is it possible to do that?我需要获取/test和/angular是否可以这样做?
Thanks in advance.提前致谢。
ngOnInit() {
let firstUrl: string = '/test/someurls';
let secondUrl:string = '/angular/someurls';
let t = firstUrl.split(" ").find(element =>element.startWith('/test');
console.log(t);
}
Here is code using split function.这是使用拆分功能的代码。
function getBeginning(a) {
return "/" + a.split("/")[1];
}
console.log(getBeginning("/test/some"));
console.log(getBeginning("/angular/some"));
Another interesting way with Angular
api: Angular
api 的另一种有趣方式:
const urlTree = this.router.parseUrl('/angular/someurls');
...
getBeginningFromUrlTree(urlTree: UrlTree): string {
return `/${urlTree.root.children.primary.segments[0].path}`;
}
crumbs = this.router.url.split('/');
constructor(private router: Router) {}
Something like this ?像这样的东西?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.