vue组件的刷新方式,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)

5.动态组件 mcake-h5分类切换 【测试成功
<template>
    <div>
        <div class="pro-top" ref='tabs'>
          <ul class="clearfix">
              <li :class='{on:navName=="cakeList"}' @click='routes("cakeList",1)'>蛋糕</li>
              <li :class='{on:navName=="breadList"}' @click='routes("breadList",11)'>面包</li>
              <li :class='{on:navName=="snackList"}' @click='routes("snackList",6)'>小食</li>
              <li :class='{on:navName=="peijianList"}' @click='routes("peijianList",5)'>配件</li>
          </ul>
        </div>
       <div class="pro-main">
          <keep-alive>
              <component :is="navName" :id='id' :isChangeList='isChangeList' :clear='clear' :isRefresh='isRefresh'></component>  
          </keep-alive>
          <!-- <cakeList :id='id' :isChangeList='isChangeList' :clear='clear' :isRefresh='isRefresh'></cakeList> -->
       </div>
    </div>
</template>

<script>
  import cakeList from '../components/cake_list'
  import breadList from '../components/bread_list'
  import snackList from '../components/snack_list'
  import peijianList from '../components/peijian_list'

  export default {
    name: 'cake',
    components: {
      cakeList,
      breadList,
      snackList,
      peijianList
    },
    data () {
      return {
        id: 1,
        isRefresh: false,
        prolist: [],
        navName: 'cakeList',
      }
    },
    props: ['isChangeList', 'clear'],
    methods:{
      routes:function(type,index){
        this.navName = type;
        this.id = index;
        console.log(index)
      }
    }
  }
</script>

<style scoped>
.pro-top {
      position: fixed;
      width: 100%;
      z-index: 99;
      padding: 0;
      top: 1.6rem;
      background-color: #fff;
      padding: 0 0.2rem;
      overflow-x: scroll;
      -webkit-overflow-scrolling: touch;
       border-bottom: 0.5px #b7b7b7 solid;
  }

  .pro-top ul {
      display: flex;
      height:1rem;
      overflow-x: scroll;
      overflow-y: hidden;
      -webkit-overflow-scrolling: touch;
  }

  .pro-top ul li {
      flex: 1;
      height: 0.5rem;
      line-height: 0.5rem;
      margin: 0.2rem 0;
      font-size: 0.4rem;
      color: #c5c5c5;
      border-right: 0.5px #b7b7b7 solid;
  }
  .pro-top ul li:last-child{
    border: none;
  }

  .pro-top ul li.on {
      position: relative;
      color: #000;
  }

.pro-main{
  margin-top: 1rem;
}
</style>

 

 







免责声明!

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



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