如何在 Angular 5 中获取当前请求的 url (window.location)?

Arn*_*eld 5 angular asp-net-core-spa-services angular5

如何在 Angular 5 中获取当前请求的 url?

我尝试了在网上找到的几个解决方案,但似乎没有任何效果。- $window - 在 WindowRef 示例中包装本机窗口

我整个早上都在寻找这个,但对 Angular 有点陌生,并且迷失在针对不同版本 Angular 的在线不同示例中。

更新:我正在使用 TypeScript

谢谢

小智 7

如果你想要角度解决方案,你可以使用这个。

import { Router } from '@angular/router';

constructor(private router:Router) {}

someMethod(){
  console.log(this.router.url)
}
Run Code Online (Sandbox Code Playgroud)

否则你可以使用标准窗口解决方案

window.location.href 返回当前页面的href (URL)。


小智 -1

要获取浏览器地址栏中的当前 URL,您可以使用 window.location.href。您不需要 Angular 包装器来获取当前 URL。