前端实现
方法一
- 在 App.vue 文件中缓存下刷新时当前页面的路由
created() {this.currentPathLoad();
},
methods: {currentPathLoad() {window.addEventListener("current-path-load",()=>{let path = this.$route.path;sessionStorage.setItem('current-path-load',JSON.stringify(path));})}
}
- 具体页面实现
methods: {someMethods() {let pagePath = this.$route.pathlet path = JSON.parse(sessionStorage.getItem('current-path-load'))if (pagePath === path) {console.log('刷新')sessionStorage.removeItem('current-path-load') } else {console.log('不是刷新')}}
}
方法二
mounted(){if(window.name == ""){console.log("首次被加载");window.name = "isRefresh"; }else if(window.name == "isRefresh"){console.log("页面被刷新");}
}
方法三
mounted(){if (window.performance.navigation.type == 1) {console.log("页面被刷新")}else{console.log("首次被加载")}
}