加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 08:04:14 所属栏目:安全 来源:网络整理
导读:我有一个组件,我需要检测用户是否在他的浏览器中按下按钮以导航回来。 目前我正在订阅路由器事件。 constructor(private router: Router,private activatedRoute: ActivatedRoute) { this.routerSubscription = router.events .subscribe(event = { // if (
我有一个组件,我需要检测用户是否在他的浏览器中按下按钮以导航回来。

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

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!');

    });

}

(...)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读