1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>14.vue_set方法</title> <script src="../js/vue.js"></script> <style> #root { display: flex; justify-content: space-evenly; } .conditiondv { width: 200px; height: 200px; background-color: bisque; } [v-cloak] { display: none; } </style> </head> <body> <div id="root">
<div> <span>对象动态添加属性</span> <br /> <span >this.$set(data.object,'prototypeName','prototypeValue')</span > <span >Vue.set(data.object,'prototypeName','prototypeValue')</span > <ul> <li v-for="(v,k) in student"> <span>key:{{k}}</span>----<span>value:{{v}}</span> </li> </ul> <button @click="addPrototypeClick">add prototype</button> </div>
<div> <span>对象动态修改数组属性</span><br /> <span >this.$set(data.object,'prototypeName','prototypeValue')</span > <ul> <li v-for="(v,k) in student"> <span>key:{{k}}</span>----<span>value:{{v}}</span> </li> </ul> <button @click="addHobbyClick">add prototype</button> <br /> <button @click="changeHobbyClick">使用数组的变更方法</button> <table border="1"> <thead> <td>方法</td> <td>使用</td> <td>描述</td> </thead> <tbody> <tr> <td>push</td> <td> const length = arrayObj. push([item1 [item2 [. . . [itemN ]]]]) </td> <td> 将一个或多个新元素添加到数组结尾,并返回数组新长度 </td> </tr> <tr> <td>pop</td> <td>const obj = arrayObj.pop()</td> <td>移除最后一个元素并返回该元素值</td> </tr> <tr> <td>shift</td> <td>const obj = arrayObj.shift()</td> <td> 移除最前一个元素并返回该元素值,数组中元素自动前移 </td> </tr> <tr> <td>unshift</td> <td> const length = arrayObj.unshift([item1 [item2 [. . . [itemN ]]]]) </td> <td> 将一个或多个新元素添加到数组开始,数组中的元素自动后移,返回数组新长度 </td> </tr> <tr> <td>splice</td> <td> const deleteArr = arrayObj.splice(deletePos,deleteCount,[newItem1,newItem2]) </td> <td> 删除从指定位置deletePos开始的指定数量deleteCount的元素[并添加一个或多个新的元素],数组形式返回所移除的元素 </td> </tr> <tr> <td>sort</td> <td>const sortArr = arrayObj.sort()</td> <td> 反转元素(最前的排到最后、最后的排到最前),返回数组地址 </td> </tr> <tr> <td>reverse</td> <td>const reverseArr = arrayObj.reverse()</td> <td> 反转元素(最前的排到最后、最后的排到最前),返回数组地址 </td> </tr> </tbody> </table> </div> </div> </body> <script> Vue.config.productionTip = false; const vm = new Vue({ el: '#root', data() { return { student: { name: 'odinsam', age: 20, hobby: ['抽烟', '喝酒', '烫头'] } }; }, methods: { addPrototypeClick() { console.log('function addPClick'); this.$set(this.student, 'sex', '男'); }, addHobbyClick() { console.log('function addHobbyClick'); this.$set( this.student.hobby, this.student.hobby.length, '学习-' + this.student.hobby.length ); }, changeHobbyClick() { console.log('function changeHobbyClick'); this.student.hobby.push( 'push new hobby-' + this.student.hobby.length ); } } }); </script> </html>
|