How do I detect user navigating back in Angular2?

前端 未结 6 1766
离开以前
离开以前 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:41

    As thorin87 answer dont use PlatformLocation. We need subscribe an unsubscribe.

    import {Subscription} from 'rxjs/Subscription';    
    
    ngOnInit() {
      this.subscription = this
        .location
        .subscribe(() => x => console.log(x));
    }
    
    ngOnDestroy() {
      this.subscription.unsubscribe();
    }
    

提交回复
热议问题