[英]Angular 2 Activated Route
也許我錯過了什么?
在 Angular 2 組件中,我只是想獲得完整的激活路由。
我不想使用location.href
。
我的構造函數如下:
constructor(private route: ActivatedRoute) {}
在ngOnInit()
我嘗試在這里找到值但無濟於事:
this.route.data.url
您不需要為此使用ActivatedRoute
,您需要使用Router以字符串的形式查詢當前 URL:
constructor(r: Router) {
console.log(r.url);
}
一個更簡單的解決方案是使用@angular/common
LocationStrategy 類直接從瀏覽器的 URL 表示和讀取路由狀態,這在嘗試以字符串形式獲取路由器 URL 時比this.router.url
更方便。
import {LocationStrategy} from '@angular/common';
export class MyComponent implements OnInit {
constructor(private url:LocationStrategy) { }
ngOnInit() {
//this will log the current url
console.log(this.url.path());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.