How do I detect user navigating back in Angular2?

前端 未结 6 1764
离开以前
离开以前 2020-11-27 15:01

I have a component and I need to detect if user pressed back button in his browser to navigate back.

Currently I\'m subscribing router events.

constr         


        
6条回答
  •  悲哀的现实
    2020-11-27 15:47

    import { HostListener } from '@angular/core';
    

    and then listen for popstate on the window object:

      @HostListener('window:popstate', ['$event'])
      onPopState(event) {
        console.log('Back button pressed');
      }
    

    This code works for me on latest Angular 2.

提交回复
热议问题