How to use $set to change the entire array
data: { arr: [1,2,3,4] }, methods: { fun: function(){ //改变一个元素 this.$set(this.arr, 2, 'a3') //改变整个数组 应该怎么写 //this.arr = [9,8,7] } }
Direct assignmentthis.arr = ['h', 'e', '...'];
methods: { fun() { this.$set(this, arr, ['a', 'b', 'c']) }, fun2() { this.arr = ['a', 'b', 'c'] }, fun3() { this.arr.splice(0, this.arr.length, 'a', 'b', 'c') } }
$set is generally used to add key-value pairs to objects. Arrays can be directly assigned values. For data operations, js-related operations will be monitored.
Direct assignment
this.arr = ['h', 'e', '...'];
$set is generally used to add key-value pairs to objects. Arrays can be directly assigned values. For data operations, js-related operations will be monitored.