Angular 2 Get Current Route


Answer :

Try this,

import { Router } from '@angular/router'; export class MyComponent implements OnInit {      constructor(private router:Router) { ... }      ngOnInit() {         let currentUrl = this.router.url; /// this will give you current url          // your logic to know if its my home page.     }  } 

Try it

import { Component } from '@angular/core'; import { Router, NavigationEnd } from '@angular/router';  @Component({...})  export class MyComponent {    constructor(private router:Router) {      router.events.subscribe(event => {        if (event instanceof NavigationEnd ) {         console.log("current url",event.url); // event.url has current url         // your code will goes here       }     });   } } 

Try any of these from the native window object.

console.log('URL:' + window.location.href); console.log('Path:' + window.location.pathname); console.log('Host:' + window.location.host); console.log('Hostname:' + window.location.hostname); console.log('Origin:' + window.location.origin); console.log('Port:' + window.location.port); console.log('Search String:' + window.location.search); 

NOTE: DO NOT USE THIS IN SERVER SIDE RENDERING


Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?