Vue 监听手机返回键关闭弹窗
popstate 事件
a.当活动历史记录条目更改时,将触发 popstate 事件。
b.如果被激活的历史记录条目是通过对 history.pushState() 的调用创建的,或者受到对 history.replaceState() 的调用的影响,
popstate 事件的 state 属性包含历史条目的状态对象的副本。
c.需要注意的是调用 history.pushState()
或 history.replaceState() 不会触发popstate事件。
d.只有在做出浏览器动作时,才会触发该事件,如用户点击浏览器的回退按钮(或者在Javascript代码中调用
history.back())
实现代码
1. 配置路由 meta 的 keepAlive 属性,再 App.vue 中 使用 keep-alive 标签 将当前页面加入缓存中
<keep-alive>
<router-view v-wechat-title="$route.meta.title" v-if="$route.meta.keepAlive">
<!-- 这里是会被缓存的试图组件 -->
</router-view>
</keep-alive>
&