Angular 路线在选中时为空
Angular routes empty when checked
我正在尝试获取我的 Angular 应用程序的当前路径。但是,这总是返回空的。
constructor(
private route: ActivatedRoute
) {
this.route.url.subscribe(segments => {
var currentPath = segments[0].path;
console.log("Current Route: ", currentPath);
});
}
无论我导航到哪里,这总是空的:
我做错了什么?
ActivatedRoute
没有路径。您应该咨询可以与构造函数一起注入的路由器。
一种常见的方法是提供用于导航到已激活路线的位置,但它会使用您可能不遵守的其他服务。
如果你想得到当前路线url,你可以使用这个代码:
import { Router, ActivatedRoute } from '@angular/router';
constructor(
private router: Router,
private activatedRoute: ActivatedRoute) {
console.log(activatedRoute.snapshot.url[0].path);
}
如果您想获得完整的 url 网络应用程序。你应该使用这个:
window.location.pathname
我正在尝试获取我的 Angular 应用程序的当前路径。但是,这总是返回空的。
constructor(
private route: ActivatedRoute
) {
this.route.url.subscribe(segments => {
var currentPath = segments[0].path;
console.log("Current Route: ", currentPath);
});
}
无论我导航到哪里,这总是空的:
我做错了什么?
ActivatedRoute
没有路径。您应该咨询可以与构造函数一起注入的路由器。
一种常见的方法是提供用于导航到已激活路线的位置,但它会使用您可能不遵守的其他服务。
如果你想得到当前路线url,你可以使用这个代码:
import { Router, ActivatedRoute } from '@angular/router';
constructor(
private router: Router,
private activatedRoute: ActivatedRoute) {
console.log(activatedRoute.snapshot.url[0].path);
}
如果您想获得完整的 url 网络应用程序。你应该使用这个:
window.location.pathname