How to get the current url in the browser in Angular 2 using TypeScript?

后端 未结 6 1007
情话喂你
情话喂你 2020-12-05 23:34

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

In JavaScript normally we do it using the window object.

How can I do this

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-05 23:37

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

提交回复
热议问题