网页设计与网站建设区别,温建设文件发布在哪个网站,广西智能网站建设哪家好,wordpress图片站我修改被v-for绑定的数据#xff0c;发现居然不能重新渲染。
查找后得知一下方法:
$set 是 Vue 提供的一个全局方法#xff0c;用于向响应式对象中添加或更新属性#xff0c;并触发视图更新。它接受三个参数#xff1a;对象、要添加/更新的属性名或索引#xff0c;以及新…我修改被v-for绑定的数据发现居然不能重新渲染。
查找后得知一下方法:
$set 是 Vue 提供的一个全局方法用于向响应式对象中添加或更新属性并触发视图更新。它接受三个参数对象、要添加/更新的属性名或索引以及新的值。 // 参数1:绑定数组/对象,参数2: 索引/key, 参数3: 被修改的值
this.$set(this.clsBut[ind], 1, true);
(他虽然这样说,但是我直接修改数组和对象元素是能够重新渲染但是v-for渲染数据不可以。)
案例代码:
!DOCTYPE html
html langenheadmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleVue/titlestyle.bg {background-color: rgb(255, 0, 0);}/style
/headbodydiv iddemoinput typebutton :valueitem[0] v-for(item,index) in clsBut :class{bg:item[1]}clickactive(item[0]){{clsBut}}/divscript srchttps://cdn.jsdelivr.net/npm/vue2.7.14/dist/vue.js/script
/bodyscript typetext/javascriptVue.config.productionTip falsenew Vue({el: #demo,data() {return {name: qhx,address: 成都,clsBut: [[1, false], [2, false], [3, false], [4, false]],};},methods: {active(index) {let clsButs this.clsButfor (const ind in clsButs) {if (clsButs[ind][0] index) {console.log(clsButs[ind][0] index);this.$set(this.clsBut[ind], 1, true);} else {this.$set(this.clsBut[ind], 1, false);}}this.clsBut clsButs}},})/script