vue实现局部刷新
使用2.2.0 新增的provide / inject控制<router-view>的显示隐藏
在App.vue中使用provide
//App.vue
<template>
<div>
<router-view v-if="isRouterAlive"></router-view>
</div>
</template>
<script>
export default {
name: 'App',
data () {
return {
isRouterAlive: true
}
},
provide(){ //提供
return {
reload: this.reload
}
},
methods: {
reload(){
this.isRouterAlive = false
this.$nextTick( function () {
this.isRouterAlive = true
})
}
}
}
</script>在使用局部刷新的组件中使用inject
<script>
export default {
name: 'myComponent',
data () {
return {}
},
inject: ['reload'], //注入
methods: {
myCallBack(){
// ...
this.reload() //局部刷新
}
}
}
</script>其他的刷新页面方法
window.location.reload() //有白屏默认参数是 false,它会用 HTTP 头 If-Modified-Since 来检测服务器上的文档是否已改变;
如果文档已改变,reload() 会再次下载该文档;
如果文档未改变,则该方法将从缓存中装载文档。这与用户单击浏览器的刷新按钮的效果是完全一样的。 参数为 true,无论文档的最后修改日期是什么,它都会绕过缓存,从服务器上重新下载该文档。这与用户在单击浏览器的刷新按钮时按住 Shift 健的效果是完全一样
this.$router.go(0) //有白屏先跳转到一个空白页面再跳转回来 //虽不会一闪,但是能看见路由快速变化
//需要页面刷新的地方,跳转到一个空白页
this.$router.push('/emptyPage')
//空白页
beforeRouteEnter (to, from, next) {
next(vm => {
vm.$router.replace(from.path)
})
}本文内容仅供个人学习/研究/参考使用,不构成任何决策建议或专业指导。分享/转载时请标明原文来源,同时请勿将内容用于商业售卖、虚假宣传等非学习用途哦~感谢您的理解与支持!