Angular 11 Get Current Route Url Example
In this article, We will explain to you how to get the current route URL in angular 11(Angular 11 Get Current Route Url Example). there are many types of example through you can get current route URL.
If you want the current route URL or path then you can use the two ways, such as Window Location and Router Url. Which we will explain to you with an example. so you can see our example.
Using Window Location
In this example, we can easily get the current route Url using the window location in angular. so you can see examples.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import { Component } from '@angular/core'; import { Router } from '@angular/router'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { constructor() { } ngOnInit() { console.log( window.location.href); } } |
Using Router Url
In this example, we can easily get the current route Url using the router URL in angular. so here we are using the router library for that and you can see examples.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import { Component } from '@angular/core'; import { Router } from '@angular/router'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { constructor(private router: Router) { } ngOnInit() { console.log(this.router.url); } } |
Read Also
Angular 11 CRUD Application Example Tutorial
Angular 11 Router Tutorial With Example
Angular 11 Custom Validation Tutorial With Example