获取不带参数的当前路线 [英] Get current route without parameters
本文介绍了获取不带参数的当前路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在Angular 2中获取没有参数的当前路线,我找到了一种获取具有参数的当前路线的方法,如下所示:
I need to get my current route without params in Angular 2, I found a way to get the current route with params as follows:
this.router.url
然后将其拆分:
this.router.url.split(';')[0]
但这看起来是解决方法,我认为应该有更好的方法吗?
But this looks as workaround, I think there should be better way?
推荐答案
parseTree
来自Router
,有助于在无需任何有关url结构的情况下获取分段.
parseTree
from Router
helps fetching the segments without any knowledge about url structure.
import { Router } from '@angular/router';
...
constructor(private router: Router) {}
...
const urlTree = this.router.parseUrl(url);
const urlWithoutParams = urlTree.root.children['primary'].segments.map(it => it.path).join('/');
从这里开始.如果您有辅助插座,请根据需要进行调整.
Start from here. If you have secondary outlets adjust as required.
这篇关于获取不带参数的当前路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文