Vue keepAlive页面强制刷新

需求

现在有一个需求,要求不刷新浏览器,但要刷新路由中的组件

方案

  • 将需要keepAlive的页面name加入到keep-live的include中。
  • Vue的transition组件,有一个after-enter钩子事件,待子组件插入完毕调用,正好适合这个场景,在这个钩子中,将当前组件添加到keep-live的include中。
  • 在刷新时,从keepAliveArr中移除当前组件的name,然后利用v-if删除router-view组件,在nextTick事件后将router-view添加回来,实现组件的刷新

代码

template

<transition @after-enter="afterRouterChange">
  <keep-alive :include="keepAliveArr">
    <router-view v-if="refreshControl" ref="child"/>
  </keep-alive>
</transition>
<button @click="refreshChild"></button>

script

export default {
   
  name: 'index',
  data () {
   
    return {
   
      keepAliveArr: [],
      refreshControl: true
    }
  },
  methods: {
   
    refreshChild () {
   
      // 先移除,再加载,强制刷新子页面
      const name = this.$refs.child.$options.name
      this.keepAliveArr.splice(this.keepAliveArr.indexOf(name), 1)
      this.refreshControl = false
      this.$nextTick(() => this.refreshControl = true)
    },
    afterRouterChange () {
   
      // 记录子组件name,用于keepalive
      const childName = this.$refs.child.$options.name
      this.pageTabList[this.pageTabIndex].name = childName
      if (!this.keepAliveArr.includes(childName)) {
   
        this.keepAliveArr.push(childName)
      }
    }
  }
}
全部评论

相关推荐

02-26 13:56
已编辑
重庆财经学院 Java
King987:你有实习经历,但是写的也太简单了,这肯定是不行的,你主要要包装实习经历这一块,看我的作品,你自己包装一下吧,或者发我,我给你出一期作品
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务