get current route angular

How to get current url in Angular?

In this post we will give you information about How to get current url in Angular?. Hear we will give you detail about How to get current url in Angular?And how to use it also give you demo for it if it is necessary.

Our leading topic is how to get current url in angular component. i will show you angular get current route url using router. we will get current url using angular router. you can easily get current url (route path) in angular 6, angular 7, angular 8 and angular 9.

 

we will use angular Router library to getting current url in component file. we can easily get current route path with Router.

 

We might be some time need to get current path or current router url in angular application. so if you need now then i will help you how you can get current url in angular. so you can see basic solution bellow:

See also  IMC Products price list 2024 with New MRP DP BV pdf : Free Download

 

You can get current url like as bellow, i also written full example with output so, you can understand:

 

this.router.url;

window.location.href;

 

 

Now i will give you full example. you can write code on your component file as like bellow:

 

Component File

 

import { Component, OnInit } from ‘@angular/core’;

import { Router } from ‘@angular/router’;

@Component({

selector: ‘app-posts’,

templateUrl: ‘./posts.component.html’,

styleUrls: [‘./posts.component.css’]

})

export class PostsComponent implements OnInit {

constructor(private router: Router) { }

ngOnInit() {

 

console.log(this.router.url);

console.log( window.location.href);

 

}

}

 

I hope it can help you…

Hope this code and post will helped you for implement How to get current url in Angular?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section.

Leave a Comment