zoukankan      html  css  js  c++  java
  • vue修改对象的属性值后页面不重新渲染

    原文地址:vue修改对象的属性值后页面不重新渲染

    最近项目在使用vue,遇到几次修改了对象的属性后,页面并不重新渲染,场景如下:
    
    HTML页面如下:
    
    [html] view plain copy
    <template  v-for="item in tableData">  
                  <div :class="{'redBorder':item.red}">  
                    <div>{{ item.name}}</div>  
                    <div>  
                        <el-button size="mini" @click="clickBtn(item.id)" type="info">编辑</el-button>  
                      <span style="white-space:pre;">   </span><p class="el-icon-error" v-show="item.tip"></p>  
                    </div>  
                  </div>  
    </template>  
    js部分如下:
    
    [javascript] view plain copy
    <script>  
     export default {  
          data() {  
            return {  
             tableData:[{id:0,name:"lili",red:false,tip:false}]  
            }  
          },  
      
          methods: {  
        clickBtn(id){  
            this.tableData[id].red=true;  
            this.tableData[id].tip=true;          
        }  
        }  
    }  
    </script>  
    绑定的class是加一个红色的边框,如下:
     
    
    [css] view plain copy
    .redBorder{  
        border:1px solid #f00;  
    }  
    
    在项目中点击button后不出现红色边框和提示错误框,打开debugger查看,发现运行到了这里却没有执行,tableData中的值并没有改变,这个方法在以前使用时会起作用,可能是这次的项目比较复杂引起的,具体原因不明。
    后通过查找资料修改为使用$set来设定修改值,js如下:
    [javascript] view plain copy
    this.$set(this.tableData[id],"red",true);  
    但是依然没有起作用,打开debugger发现tableData的值修改成功,没有渲染到页面上,查找的资料也是比较凌乱,并不能解决问题,后请教大神,才知道是数据层次太多,没有触发render函数进行自动更新,需手动调用,调用方式如下:
    [javascript] view plain copy
    this.$forceUpdate();  
    js完整代码如下:
    [javascript] view plain copy
    <script>  
     export default {  
          data() {  
            return {  
             tableData:[{id:0,name:"lili",red:false,tip:false}]  
            }  
          },  
      
          methods: {  
        clickBtn(id){  
            this.$forceUpdate();  
            this.$set(this.tableData[id],"red",true);  
            this.$set(this.tableData[id],"tip",true);     
     }}}  
    </script>  
    以上是我解决问题的全过程,有不对的地方请指教。
  • 相关阅读:
    Populating Next Right Pointers in Each Node II
    Populating Next Right Pointers in Each Node
    Construct Binary Tree from Preorder and Inorder Traversal
    Construct Binary Tree from Inorder and Postorder Traversal
    Path Sum
    Symmetric Tree
    Solve Tree Problems Recursively
    632. Smallest Range(priority_queue)
    609. Find Duplicate File in System
    poj3159最短路spfa+邻接表
  • 原文地址:https://www.cnblogs.com/jcz1206/p/8833908.html
Copyright © 2011-2022 走看看