vue修改数据,刷新当前页面,重新渲染页面数据

业务场景:在管理后台,在执行完,增,删,改,操作的时候。我们需要刷新一下页面,重载数据。在JQ中我们会用到location.reload()方法,刷新页面;在vue中,这里需要用到一个 provide / inject 这对用例。(其他方法:this.$router.go(0),会强制刷新,出现空白页面体验不好);

原理是:通过控制router-view 的显示与隐藏,来重渲染路由区域,重而达到页面刷新的效果。

代码如下:

1:在父组件中加入

<template>
  <div id="app">
        <router-view v-if="isRouterAlive"></router-view>
  </div>
</template> 
<script>
export default {
  name: 'App',
  provide (){
     return {
       reload:this.reload
     }
  },
  data(){
    return {
       isRouterAlive:true
    }
  },
  methods:{
    reload (){
       this.isRouterAlive = false
       this.$nextTick(function(){
          this.isRouterAlive = true
       })
    }
  },
  components:{
  }
}
</script>

2:然后在子组件export default中引入

inject:['reload']

3:最后在执行完相应的操作,后调用reload方法就可以了!

this.reload();

猜你喜欢

转载自www.cnblogs.com/finghi/p/12688619.html