Tengo urls y quiero recuperar el inicio de la ruta pero no se como hacerlo y siendo un principiante de acuerdo a mi investigación debo usar el método split() y find().
Aquí están las direcciones URL:
/test/someurls
/angular/someurls
Necesito obtener /test y /angular ¿es posible hacer eso?
Gracias por adelantado.
ngOnInit() { let firstUrl: string = '/test/someurls'; let secondUrl:string = '/angular/someurls'; let t = firstUrl.split(" ").find(element =>element.startWith('/test'); console.log(t); }Aquí está el código usando la función de división.
function getBeginning(a) { return "/" + a.split("/")[1]; } console.log(getBeginning("/test/some")); console.log(getBeginning("/angular/some"));Otra forma interesante con 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) {}Algo como esto ?