目前的文档只讨论了获取路由参数,而不是实际的路由段。
例如,如果我想找到当前路由的父,这是怎么可能的?
目前的文档只讨论了获取路由参数,而不是实际的路由段。
例如,如果我想找到当前路由的父,这是怎么可能的?
当前回答
router.events。订阅(e => { if (e instanceof NavigationEnd) { 这一点。currentUrl = e.url; } });
其他回答
我也有同样的问题
this.router.url
我用查询参数获取当前路由。我做的一个变通方法是使用这个:
this.router.url.split('?')[0]
这不是一个很好的解决方案,但很有帮助。
您可以使用ActivatedRoute来获取当前路由器
原文答案(RC版)
我在AngularJS谷歌Group上找到了一个解决方案,非常简单!
ngOnInit() {
this.router.subscribe((url) => console.log(url));
}
这是最初的答案
https://groups.google.com/d/msg/angular/wn1h0JPrF48/zl1sHJxbCQAJ
本机窗口对象也可以正常工作
console.log('URL:' + window.location.href);
console.log('Path:' + window.location.pathname);
console.log('Host:' + window.location.host);
console.log('Hostname:' + window.location.hostname);
console.log('Origin:' + window.location.origin);
console.log('Port:' + window.location.port);
console.log('Search String:' + window.location.search);
注意:不要在服务器端渲染中使用这个
角RC4:
你可以从@angular/ Router中导入Router
然后注入:
constructor(private router: Router ) {
}
然后调用它的URL参数:
console.log(this.router.url); // /routename
Angular 2 rc2
router.urlTree.contains(router.createUrlTree(['/home']))