84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
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.