优先举荐应用vue的$set赋值,能够参考vue的官网文档$set用法。
如果不行能够应用this.$forceUpdate()办法
最近遇到几次批改了对象的属性后,页面并不从新渲染,场景如下:
HTML页面如下:
<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> <p class="el-icon-error" v-show="item.tip"></p> </div> </div></template>
js局部如下:
<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是加一个红色的边框,如下:
.redBorder{ border:1px solid #f00;}
在我的项目中点击button后不呈现红色边框和提醒谬误框,关上debugger查看,发现运行到了这里却没有执行,tableData中的值并没有扭转,这个办法在以前应用时会起作用,可能是这次的我的项目比较复杂引起的,具体起因不明。
后通过查找材料批改为应用$set来设定批改值,js如下:
this.$set(this.tableData[id],"red",true);
然而仍然没有起作用,关上debugger发现tableData的值批改胜利,没有渲染到页面上,查找的材料也是比拟凌乱,并不能解决问题,后求教大神,才晓得是数据档次太多,没有触发render函数进行自动更新,需手动调用,调用形式如下:
this.$forceUpdate();
js残缺代码如下:
<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>
如有问题请多指教。