2025年01月06日 建站教程
vue开发中,如何在刷新页面后保证路由不会变化,下面web建站小编给大家简单介绍一下!
具体代码如下:
export default { created() { // 监听beforeunload事件 window.addEventListener("beforeunload", this.saveCurrentPath) }, beforeDestroy() { // 解除beforeunload事件的绑定 window.removeEventListener('beforeunload', this.saveCurrentPath) }, methods: { saveCurrentPath() { //当前路由的路径保存到localStorage localStorage.setItem('currentRoutePath', this.$route.path) } }, mounted() { const currentRoutePath = localStorage.getItem('currentRoutePath') if (currentRoutePath) { this.$router.push(currentRoutePath) //执行后清除localStorage缓存 localStorage.removeItem('currentRoutePath') } } }
本文链接:http://so.lmcjl.com/news/20835/