vue组件的刷新方式


平时在做项目的过程钟时常需要刷新组件,这里抽空总结下vue刷新组件的方式

vue刷新组件的方式:

1.给组件不同的key值,这样每次进入页面时,当key发生变化时,会释放原有组件重新加载改组件,这也是我平时最常用的方式

<group :key="new Date().getTime()">
        <datetime title="请选择时间" v-model="time"  format="YYYY-MM-DD" :start-date="start" :end-date="end"
        year-row="{value}年" month-row="{value}月" day-row="{value}日" confirm-text="完成" cancel-text="取消"
        ></datetime> 
      </group>

2.使用v-if刷新子组件,使用v-if控制改组件的显示隐藏,当点击刷新按钮时,组件需要重新显示,当v-if里的值发生改变时,组件会重新渲染,来达到强制刷新组件的方式

<button v-if="btnShow" ></button>
// 先将this.btnShow 设为false,再设为true
   this.btnShow = false
     this.$nextTick(() => (this.btnShow = true))

3.使用组件内置$forceUpdate方法,使用前需要在配置中启用

import Vue from 'vue'
Vue.forceUpdate()

然后在组件在使用  this.$forceUpdate()

4.刷新整个页面:this.router.go(0)

 

 

这里附上其他博主的博客链接,https://blog.csdn.net/qq_41913971/article/details/113937432


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM