Skip to main content

Posts

Showing posts with the label Get current URL route in Angular

How To Get Current URL/Route in Angular?

How to get the current URL/Route in Angular? With pure JavaScript: console . log ( window . location . href ) Using Angular:   this . router . url The Examples in details - import { Component } from '@angular/core' ; import { Router } from '@angular/router' ; @ Component ({     template: 'The href link is : {{href}}' }) export class Component {     public href : string = null ;     constructor ( private router : Router ) {}     ngOnInit () {         this . href = this . router . url ;         console . log ( this . href );     } } To get the route segments: import { ActivatedRoute , UrlSegment } from '@angular/router' ; constructor ( route : ActivatedRoute ) { } getRoutes () {   const segments : UrlSegment [] = this . route . snapshot . url ; } //...