如何检测用户在Angular2中导航回来?

前端之家收集整理的这篇文章主要介绍了如何检测用户在Angular2中导航回来?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个组件,我需要检测用户是否在他的浏览器中按下按钮以导航回来。

目前我正在订阅路由器事件。

constructor(private router: Router,private activatedRoute: ActivatedRoute) {

    this.routerSubscription = router.events
        .subscribe(event => {

            // if (event.navigatesBack()) ...

        });

}

我知道我可以使用window.onpopstate,但在使用Angular2时感觉就像是黑客。

可以使用具有onPopState侦听器的PlatformLocation。
import { PlatformLocation } from '@angular/common'

(...)

constructor(location: PlatformLocation) {

    location.onPopState(() => {

        console.log('pressed back!');

    });

}

(...)
原文链接:https://www.f2er.com/angularjs/144023.html

猜你在找的Angularjs相关文章