How to detect a route change in Angular?

前端 未结 22 1976
花落未央
花落未央 2020-11-22 04:40

I am looking to detect a route change in my AppComponent.

Thereafter I will check the global user token to see if he is logged in. Then I can redirect t

22条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 05:11

    Location works...

    import {Component, OnInit} from '@angular/core';
    import {Location} from '@angular/common';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.scss']
    })
    
    export class AppComponent implements OnInit {
    
        constructor(private location: Location) {
            this.location.onUrlChange(x => this.urlChange(x));
        }
    
        ngOnInit(): void {}
    
        urlChange(x) {
            console.log(x);
        }
    }
    

提交回复
热议问题