Angular 2 条激活路线
Angular 2 Activated Route
也许我遗漏了什么?
在 Angular 2 组件中,我只是想获取完整的激活路由。
我不想使用 location.href
。
我的构造函数如下:
constructor(private route: ActivatedRoute) {}
在 ngOnInit()
中,我尝试在此处查找值但无济于事:
this.route.data.url
一个更简单的解决方案是使用 @angular/common
中的 LocationStrategy class 来直接从浏览器的 URL 中表示和读取路线状态,这比 this.router.url
更方便试图以字符串形式获取路由器 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());
}
}
你不需要为此使用ActivatedRoute
,你需要使用Router以字符串的形式查询当前URL:
constructor(r: Router) {
console.log(r.url);
}
也许我遗漏了什么?
在 Angular 2 组件中,我只是想获取完整的激活路由。
我不想使用 location.href
。
我的构造函数如下:
constructor(private route: ActivatedRoute) {}
在 ngOnInit()
中,我尝试在此处查找值但无济于事:
this.route.data.url
一个更简单的解决方案是使用 @angular/common
中的 LocationStrategy class 来直接从浏览器的 URL 中表示和读取路线状态,这比 this.router.url
更方便试图以字符串形式获取路由器 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());
}
}
你不需要为此使用ActivatedRoute
,你需要使用Router以字符串的形式查询当前URL:
constructor(r: Router) {
console.log(r.url);
}