vue 刷新页面方式
遇到问题:在写后台管理系统的时候,遇到需要刷新页面
1.传统方法
location.reload();
2.router方法
this.$router.go(0);
用过的人都知道,前两者都是强制刷新页面,会出现短暂的闪烁,用户体验效果不好。
3.app方法
首先在app.vue文件中如下代码
<template>
<div id="app">
<router-view v-if="isRouterAlive"></router-view>
</div>
</template>
<script>
export default {
name: 'App',
provide () { //父组件中通过provide来提供变量,在子组件中通过inject来注入变量。
return {
reload: this.reload
}
},
data() {
return{
isRouterAlive: true //控制视图是否显示的变量
}
},
methods: {
reload () {
this.isRouterAlive = false; //先关闭,
this.$nextTick(function () {
this.isRouterAlive = true; //再打开
})
}
},
}
</script>
然后再在我们需要用到的地方调用
export default { inject:['reload'], //注入App里的reload方法 data () { return { ....... } },
this.reload();
山不在高,有仙则名。水不在深,有龙则灵。斯是陋室,惟吾德馨

浙公网安备 33010602011771号