Get current route without parameters

前端 未结 12 1388
梦如初夏
梦如初夏 2021-01-01 09:08

I need to get my current route without params in Angular 2, I found a way to get the current route with params as follows:

 this.router.url

12条回答
  •  再見小時候
    2021-01-01 09:34

    If you are using webpack, you can use the url polyfill module which comes bundled, along with the Angular Router module.

    import { Component, OnInit } from '@angular/core';
    import { Router } from '@angular/router';
    import * as url from 'url'
    
    @Component({
      selector: 'app-component',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.scss']
    })
    export class AppComponent implements OnInit {
    
      public constructor(public readonly router: Router) {}
    
      public ngOnInit() {
        const urlInfo = url.parse(this.router.url)
        console.log(urlInfo.pathname)
      }
    }
    

提交回复
热议问题