vue set

Vue 可以通过数组变异的方法可以控制数组的增减,却不能更改数组及对象,vue可以通过set方法改变数组的长度,改变某项的值,在组件中可以使用$set方法改变数组长度和某项的值

调用方法:Vue.set( target, key, value )

target:要更改的数据源(可以是对象或者数组)

key:要更改的具体数据

value :重新赋的值

如下面的例子:

html:

<textarea v-for="ite1m of arryText">{{ite1m.option}}</textarea>

js:

data(){

      return {

arryText: [{option: '123'}, {option: '345'}, {option: '567'}, {option: '789'}],

}
},

methods(){

//这里使用Vue.set,需要将Vue import子组件中

Vue.set(this.arryText,4,{option:456});
//如果是组件中可以用$set
this.$set(this.arryText,4,{option:456});

}

原文地址:https://www.cnblogs.com/zhx119/p/10090264.html