如何使用TypeScript在Angular 2中的浏览器中获取当前URL? [英] How to get the current url in the browser in Angular 2 using TypeScript?

查看:1058
本文介绍了如何使用TypeScript在Angular 2中的浏览器中获取当前URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to get the current URL present in the browser in my Angular 2 application.

在JavaScript中,我们通常使用window对象来执行此操作。

In JavaScript normally we do it using the window object.

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

How can I do this in Angular 2 using TypeScript?

谢谢。

推荐答案

这已经晚了但我想值得更新。从Angular2最终版本开始,您可以从@ angular / common导入DOCUMENT并使用它来到达该位置。

This is late but I thought it was worth updating. As of Angular2 final release you can import DOCUMENT from @angular/common and use that to get to the location.

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);
    }
}

这篇关于如何使用TypeScript在Angular 2中的浏览器中获取当前URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆