关于vue项目刷新当前页面,我试了一些方法,最终找到了一个目前我所知道最棒的方法。
记录如下:
在App.vue声明reload方法,控制router-view的显示或隐藏,从而控制页面的再次加载
在App.vue页面代码:
<template>
<div id="app">
<router-view v-if="isRouterAlive"></router-view>
</div>
</template>
<script>
export default {
name:'app',
provide :function() {
return {
reload:this.reload
}
},
data:function(){
return {
isRouterAlive:true
}
},
methods:{
reload:function(){
this.isRouterAlive=false;
this.$nextTick(function(){
this.isRouterAlive=true
})
}
}
}
</script>
在使用页面,需要引入在App.vue页面定义的reload方法
inject:['reload'], //注入reload方法
使用此方法:
// 退出登录
logout() {
// sessionStorage清零
window.sessionStorage.clear()
// 页面刷新
this.reload()
},