脚本宝典收集整理的这篇文章主要介绍了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>
默认参数是 false,它会用 HTTP 头 If-Modified-since 来检测服务器上的文档是否已改变;
如果文档已改变,reload() 会再次下载该文档;
如果文档未改变,则该方法将从缓存中装载文档。这与用户单击浏览器的刷新按钮的效果是完全一样的。
参数为 true,无论文档的最后修改日期是什么,它都会绕过缓存,从服务器上重新下载该文档。这与用户在单击浏览器的刷新按钮时按住 Shift 健的效果是完全一样
//需要页面刷新的地方,跳转到一个空白页
this.$router.push('/emptyPage')
//空白页
beforeRouteEnter (to, from, next) {
next(vm => {
vm.$router.replace(from.path)
})
}
以上是脚本宝典为你收集整理的vue实现局部刷新全部内容,希望文章能够帮你解决vue实现局部刷新所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。