Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用TypeScript在Angular 2中获取浏览器中的当前url?_Javascript_Typescript_Angular - Fatal编程技术网

Javascript 如何使用TypeScript在Angular 2中获取浏览器中的当前url?

Javascript 如何使用TypeScript在Angular 2中获取浏览器中的当前url?,javascript,typescript,angular,Javascript,Typescript,Angular,我需要在Angular 2应用程序中获取浏览器中的当前URL 在JavaScript中,我们通常使用窗口对象来完成 如何在Angular 2中使用TypeScript执行此操作 谢谢。你可以 注入Location并通过Location.path()获取URL(另请参阅) 使用window.location.pathname 另见 这很晚了,但我认为值得更新。从Angular2最终版本开始,您可以从@angular/common导入文档并使用该文档到达该位置 import { Componen

我需要在Angular 2应用程序中获取浏览器中的当前URL

在JavaScript中,我们通常使用窗口对象来完成

如何在Angular 2中使用TypeScript执行此操作

谢谢。

你可以

  • 注入
    Location
    并通过
    Location.path()
    获取URL(另请参阅)
  • 使用
    window.location.pathname
另见


这很晚了,但我认为值得更新。从Angular2最终版本开始,您可以从@angular/common导入文档并使用该文档到达该位置

import { Component, Inject } from '@angular/core';
import { DOCUMENT } from '@angular/common';

...

export class YourComponent {

    constructor(@Inject(DOCUMENT) private document: Document) { 
        console.log(this.document.location.href);
    }
}

导入ActivatedRoute(如果需要,还可以导入其他路由器内容)

确保它被注入到构造函数中

constructor(private route: ActivatedRoute) { ... }
在ngOnInit上,您可以使用
this.route
检查URL。例如,所有段都在一个数组中,您可以按照@ibgib的建议将其串在一起,如下所示:

let path = this.route.snapshot.url.join('/');

给你一些类似“火星/卫星/火卫一”的东西。

给未来的旅行者,很多其他的答案都很好。另一个选项,如果您希望所有内容都位于路径名之前(例如
https://example.com
)然后您可以使用:

window.location.origin

这是一篇关于不同
窗口的W3文章。您可以使用的位置
属性:


干杯

不需要导入复杂的包或注入一些东西。只需使用您可以在
窗口中找到的方法。位置

例如:

  • window.location.href
    提供完整的URL
  • window.location.hostname
    提供主机名
  • window.location.origin
    使用此命令,您可以使用协议(例如https://)获取主机名
  • 您可以在这里看到更多信息:
对于IE来说,这帮助了我:

  public static getCurrentAbsoluteSiteUrl(): string {
    if (window
        && "location" in window
        && "protocol" in window.location
        && "pathname" in window.location
        && "host" in window.location) {
      return window.location.protocol + "//" + window.location.host + window.location.pathname;
    }
    return null;
}

同样的意愿也应该适用于中国you@KaushikThanki谢谢它起作用了。愚蠢的问题我想:P应该试一下……谢谢……它能用!!但是,当我尝试使用location.path()时,它没有显示任何内容。。。不管怎样,如果我直接使用窗口对象,它就会工作。。再次感谢:)您注入了
构造函数(location:location){console.log(location.path();}
?是的。
构造函数(location:location){alert(location.prepareeexternalur(location.path());console.log(location.path());}
我个人喜欢这个版本。但是,
this.route.url
是一个
可观察的
。也许不用
forEach
来枚举可能产生多次迭代的可观察的,只需使用
this.route.snapshot.url.join('/')
取而代之?谢谢@ibgib-在回答中使用了您的代码。更为惯用。嗯,使用此方法时,即使我的url不是根,我也会得到空路径。这不起作用。错误与:ReferenceError:window不起作用defined@Neurothustra你可以试着打开一个新的问题并发布你的代码。我猜这是比赛条件某种类型。窗口被烘焙到浏览器本身,所以我猜这是一个本地问题。
导出“文档”在“@angular/common”中找不到。
4.1.3
@Deilan开始,我快速检查了源代码,它仍然存在。文档方法的问题是,您无法在测试中正确地模拟它。必须将模拟注入您的测试中r测试。请注意,不建议直接将window与Angular一起使用,因为在预渲染时,它在服务器端不可用,因此会破坏Angular Universal(如果您计划拥有一个SEO友好的站点,这一点很重要)。
  public static getCurrentAbsoluteSiteUrl(): string {
    if (window
        && "location" in window
        && "protocol" in window.location
        && "pathname" in window.location
        && "host" in window.location) {
      return window.location.protocol + "//" + window.location.host + window.location.pathname;
    }
    return null;
}