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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
| <!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>13.内置指令</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>v-text</span> <span v-text="vtext">span原有内容</span> <br /> <span>v-html</span> <span v-html="vhtml"></span> <br /> <span>v-cloak</span> <span v-cloak >当 vue 实例创建完毕并接管容器后会移除 v-cloak 属性</span > <br /> <span>v-once</span> <span v-once>初始onceNum为 {{onceNum}}</span> <span>onceNum当前值为 {{onceNum}}</span> <button @click="btnAddNum">btnAddNum</button> <br /> <span v-pre >跳过所在节点的编译过程,vue不在编译节点内容提升性能</span > </div>
<div> <span>条件渲染</span> <div v-show="vshow" class="conditiondv">v-show="vshow"</div> <button @click="vshowBtnClick">v-show</button><br /><br /> <div v-if="vif" class="conditiondv">v-if="vif"</div> <button @click="vifBtnClick">v-show</button> <div v-if="num===1" class="conditiondv">v-if="num===1"</div> <div v-else-if="num===2" class="conditiondv"> v-else-if="num===2" </div> <div v-else class="conditiondv">v-else</div> <button @click="conditionClick">conditionClick</button> </div>
<div> <span>遍历数组 v-for="item in array" :key="item.id" </span> <ul> <li v-for="(user,index) in users" :key="user.id"> index:{{index}} id:{{user.id}} 姓名:{{user.name}} 年龄:{{user.age}} </li> </ul> <span>遍历对象 v-for="(v,k) in object" :key="k"</span> <ul> <li v-for="(v,k) in users[0]" :key="k"> key:{{k}} value:{{v}} </li> </ul> <span >遍历字符串 v-for="(char,index) in string" :key="index"</span > <ul> <li v-for="(char,index) in users[0].name" :key="index"> char:{{char}} index:{{index}} </li> </ul> <span >遍历数值(次数) v-for="(num,index) in number" :key="index"</span > <ul> <li v-for="(number,index) in 5" :key="index"> number:{{number}} index:{{index}} </li> </ul> </div> <div>
<span>自定义指令</span> <span>customNum:{{customNum}}</span> <br /> <span>放大10倍后的值:</span><span v-big="customNum"></span> <br /> <button @click="btnCustomNumAdd">btnCustomNumAdd</button> </div> </div> </body> <script> Vue.config.productionTip = false; const vm = new Vue({ el: '#root', data() { return { customNum: 5, onceNum: 1, num: 1, vtext: '<span>vtext指令</span>', vhtml: '<a href="http://odinsam.com">v-html容易导致xss攻击</a>', vshow: true, vif: true, users: [ { id: 'no1', name: 'odinsam1', age: 29 }, { id: 'no2', name: 'odinsam2', age: 14 }, { id: 'no3', name: 'odinsam3', age: 26 } ] }; }, methods: { vshowBtnClick() { console.log('function vshowBtnClick'); this.vshow = !this.vshow; }, vifBtnClick() { console.log('function vifBtnClick'); this.vif = !this.vif; }, conditionClick() { this.num++; if (this.num > 3) this.num = 1; }, btnAddNum() { this.onceNum++; }, btnCustomNumAdd() { this.customNum++; } }, directives: { big: { bind(ele, binding) { console.log( '1次调用 - 当指令与元素绑定成功时调用,在内存,页面并没有元素' ); }, inserted(ele, binding) { console.log( '1或n次调用 - 指令所在的元素被插入页面时调用' ); }, update(ele, binding) { console.log('1或n次调用: 当指令所在模板被重新解析时'); } } } }); </script> </html>
|