如何检测用户在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!'); }); } (...) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- webservice学习笔记(一) - 基于JAX-WS的入门示例
- 触发自定义验证指令后,AngularJS ng-model值将丢失
- scala – 由于广泛的递归类型参数,关于类图的编译器错误并不
- angular – RxJs 6:从Observable获取ConnectableObservabl
- angularjs – 为什么此资源在使用$delete方法后没有更新视图
- angular系列之生命周期钩子
- angular – Angualr2错误:无法设置仅具有getter的#的属性值
- angular – 即使CORS设置正确,也会在请求的资源上出现“No’
- 用于GWT的Twitter Bootstrap不起作用
- 如何在Angular 2中动态添加提供程序到注入器?