【发布时间】:2020-09-15 23:17:33
【问题描述】:
我有一个角站点,我在其中插入了一些代码,以使浏览器后退按钮的行为方式与后退 UI 按钮相同。但是,当我按回浏览器时,最后访问的页面会在默认行为执行之前闪烁一秒钟。
这是一个视频来说明我的意思 - 最后我展示了单击 UI 按钮的操作,这是我想要模拟的: Browser Back Button Demo
这是我在 Angular 中使用的代码:
@HostListener('window:popstate', ['$event'])
onBrowserBackBtnClose(event: Event) {
// Stop default behavior of the back button
event.stopPropagation();
event.preventDefault();
// If we can go back...
if (this.canGoBack) {
// Load the previous question
this.questionService.previousQuestion().subscribe(res => {
// Activate the question route in place of the previous page
this.router.navigate(['question'], {replaceUrl: true});
});
}
}
我的理解是 event.stopPropagation(); 和 event.preventDefault(); 停止了这种闪烁行为?或者还有什么我不理解或遗漏的?
【问题讨论】:
-
不是看
window:popstate',而是看'window:beforeunload'呢? -
活动
window:popstate是不可取消的,所以你总是会看到最后一页。
标签: angular typescript