使用Router(this.router.url)时/MyRouterName?type=abc,/MyRouterName如果没有参数,我会得到字符串
我是否只能/MyRouterName从完整的URL 获取路径(对于所有情况)?
use*_*376 27
您可以使用激活的路径组件的快照来获取此URL.
import { Router, ActivatedRoute } from '@angular/router';
constructor(
private router: Router,
private activatedRoute: ActivatedRoute) {
console.log("routes");
console.log(activatedRoute.snapshot.url); // array of states
console.log(activatedRoute.snapshot.url[0].path);
}
Run Code Online (Sandbox Code Playgroud)
链接到原始的SO答案由Jose G Varanam 如何获得当前路线
这适合我.
导入此:
import { Router } from '@angular/router';
Run Code Online (Sandbox Code Playgroud)
然后将它传递给构造函数:
path: any;
constructor(private router: Router) {
this.path = this.router.url;
console.log(this.path);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
用户5049376给出的答案对我不起作用.url属性未定义.
console.log(activatedRoute.snapshot.url[0].path);
Run Code Online (Sandbox Code Playgroud)
而不是我使用firstChild属性.
console.log(this.activatedRoute.snapshot.firstChild.url[0].path);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51337 次 |
| 最近记录: |