今天刚好需要用到当前浏览器的url刚好记录一下,目前项目使用的是angular6,可以使用Location包也可以使用PlatformLocation,下面是两个对象打印出来的数据:

Location:

Angular6 获取当前浏览器地址

PlatformLocation:

Angular6 获取当前浏览器地址

从两个对象其实就可以看出BrowserPlatformLocation(PlatformLocation)是Location的一个子级,两个都可以获取到我们需要的浏览器数据,但是很明显BrowserPlatformLocation才是我们最终需要的。下面附上代码:

import { PlatformLocation} from '@angular/common';

for (const i in this.location) {

if ( i === 'location') {

this.locationUrl = this.location[i].origin;

break;

}

}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-09
  • 2022-01-01
  • 2021-11-29
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-21
  • 2021-06-26
  • 2021-10-26
  • 2021-09-07
相关资源
相似解决方案