2019-04-24 监听页面回退,清除eventListener

监听页面回退

问题:小程序跳转到公众号的某个页面链接,中间经过授权页面,跳过去以后再回退会回到授权页。

解决方案:

1
2
3
4
5
6
7
8
9
10
11
created() {
if (window.history && window.history.pushState) {
window.addEventListener("popstate", this.goBack, { once: true });
// 这个once表示 listener 在添加之后最多只调用一次。如果是 true, listener 会在其被调用之后自动移除
// 本来是计划在destroyed里面remove这个listener,但是似乎还没生效就destroy了,所以只能这样了
}
}

goBack() {
this.$router.go(-1);
}

遗留问题:能不能还是在destroyed里面remove这个listener?

清除eventListener

1
2
3
destroyed() {
window.removeEventListener("devicemotion", this.shakeListner, false);
}

本文标题:2019-04-24 监听页面回退,清除eventListener

文章作者:陈宸

发布时间:2019年05月05日 - 16:56

最后更新:2020年02月25日 - 11:10

原始链接:https://realcjames.github.io/2019/05/05/2019-04-24%20%E7%9B%91%E5%90%AC%E9%A1%B5%E9%9D%A2%E5%9B%9E%E9%80%80%EF%BC%8C%E6%B8%85%E9%99%A4eventListener/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------本文结束感谢您的阅读-------------