使用this.$router.go(0)方法刷新页面会造成全页面刷新,有的需求并不建议使用路由刷新。
首先图解需求:

选择使用 provide / inject方法实现:
全局页面中
<keep-alive v-if="isReloadData">
<router-view/>
</keep-alive>
data() {
return {
isReloadData: true,
}
}
//暴露父页面的方法
provide() {
return {
reload: this.reload
};
},
methods:{
//刷新事件
reload() {
this.isReloadData = false;
this.$nextTick(() => {
this.isReloadData = true;
});
}
}
子页面(带有刷新按钮的页面)
<div class="btn-fullscreen" @click="refresh">
<el-tooltip effect="dark" content="刷新" placement="bottom">
<i class="el-icon-refresh-left"></i>
</el-tooltip>
</div>
export default {
inject:['reload'],
methods: {
//点击刷新
refresh(){
// 刷新单页面
this.reload();
}
}
}
这样就实现了局部子组件刷新了。
本文介绍了一种在Vue.js中不使用this.$router.go(0)进行全页面刷新的替代方案,通过provide/inject和keep-alive组件实现局部子组件的数据刷新。详细展示了如何在父组件中提供刷新方法,并在子组件中调用该方法来刷新数据。

3万+

被折叠的 条评论
为什么被折叠?



