您好,欢迎来到宝玛科技网。
搜索
您的当前位置:首页vue实现局部刷新的实现示例

vue实现局部刷新的实现示例

来源:宝玛科技网


利用Vue里面的provide+inject组合(走过路过,不要错过)

使用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)
     })
    }
    
    

    Copyright © 2019- baomayou.com 版权所有 赣ICP备2024042794号-6

    违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

    本站由北京市万商天勤律师事务所王兴未律师提供法律服务