首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

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

在Angular 5中,可以通过使用Angular的内置服务Location来获取当前请求的URL。Location服务提供了一些方法来获取和操作浏览器的URL。

要获取当前请求的URL,可以按照以下步骤进行操作:

  1. 首先,在你的组件中导入Location服务:
代码语言:typescript
复制
import { Location } from '@angular/common';
  1. 在组件的构造函数中注入Location服务:
代码语言:typescript
复制
constructor(private location: Location) { }
  1. 然后,使用Location服务的path()方法来获取当前请求的URL:
代码语言:typescript
复制
const currentUrl = this.location.path();

path()方法返回的是当前URL的路径部分,不包括主机名和查询参数。

完整的代码示例:

代码语言:typescript
复制
import { Component } from '@angular/core';
import { Location } from '@angular/common';

@Component({
  selector: 'app-your-component',
  template: `
    <p>Current URL: {{ currentUrl }}</p>
  `
})
export class YourComponent {
  currentUrl: string;

  constructor(private location: Location) {
    this.currentUrl = this.location.path();
  }
}

关于Angular的Location服务的更多信息,你可以参考腾讯云的文档:Location 服务

请注意,以上答案中没有提及亚马逊AWS、Azure、阿里云、华为云、天翼云、GoDaddy、Namecheap、Google等流行的云计算品牌商,以符合问题要求。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券