一、插值
1.1 +号运用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../statics/vue.min.js"></script> </head> <body> <div id="app">{{ greeting + greeting }}</div> <script> new Vue({ el: "#app", data: { greeting: "Hello world" } }) </script> </body> </html>
二、v-text
类似双大括号语法渲染数据的另一种方式是使用v-text。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../statics/vue.min.js"></script> </head> <body> <div id="app" v-text="greeting"></div> <script> new Vue({ el: "#app", data: { greeting: "Hello world" } }) </script> </body> </html>
三、v-html
双大括号语法无法渲染HTML标签,我们需要使用v-html。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../statics/vue.min.js"></script> </head> <body> <div id="app" v-html="greeting"></div> <script> new Vue({ el: "#app", data: { greeting: "<h1>Hello world</h1>" } }) </script> </body> </html>
四、v-for
接下来,我们看看数组和对象的渲染方式。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../statics/vue.min.js"></script> </head> <body> <div id="app" > <ul> <li v-for="item in guniang">{{ item }}</li> </ul> <ul> <li v-for="item in students">{{ item.name }}的爱好是{{ item.hobby }}</li> </ul> <ul> <li v-for="item in student">{{ item }}</li> </ul> </div> <script> new Vue({ el: "#app", data: { guniang: ['逛街', '美甲', '吃'], students: [ { name: "jiaxiaoliang", hobby: 'girls' }, { name: "zhaofengfeng", hobby: 'girls' }, { name: 'xiaohe', hobby: 'boys' } ], student: { name: 'alex', age: 87 } } }) </script> </body> </html>
五、v-if
渲染数据的时候,同样也可以使用条件判断,我们来看看。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../statics/vue.min.js"></script> </head> <body> <div id="app" > <div v-if="name == 'guniang'"> 欢迎美女光临~~ </div> <div v-else-if="name == 'pizza'"> 欢迎帅哥光临 </div> <div v-else> 滚~~ </div> </div> <script> new Vue({ el: "#app", data: { name: "guniang" } }) </script> </body> </html>
通过上面的代码我们可以看出,v-if的作用是控制标签的显示,它通过判断添加标签,底层采用的是appendChild来实现的,下面我们来看一个同样也是控制标签显示的另一个指令v-show。
六、v-show
与v-if不同的是,v-show通过样式的display控制标签的显示。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../statics/vue.min.js"></script> <style> .box { height: 100px; 100px; background-color: red; } </style> </head> <body> <div id="app" > <div v-show="isShow" class="box">xxxx</div> </div> <script> new Vue({ el: "#app", data: { isShow: true } }) </script> </body> </html>
与v-if不同的是,v-show通过样式的display控制标签的显示。
v-if和v-show的性能比较
我们简单比较一下二者的区别:
实现方式:v-if底层采用的是appendChild来实现的,v-show通过样式的display控制标签的显示,正因为实现方式上面有差异,导致了他们的加载速度方面产生了差异;
加载性能:v-if加载速度更快,v-show加载速度慢
切换开销:v-if切换开销大,v-show切换开销小
v-if是惰性的,它是“真正”的条件渲染,因为它会确保在切换过程中条件块内的事件监听器和子组件适当地被销毁和重建,v-if 也是惰性的:如果在初始渲染时条件为假,则什么也不做——直到条件第一次变为真时,才会开始渲染条件块。
v-show 就简单得多——不管初始条件是什么,元素总是会被渲染,并且只是简单地基于 CSS 进行切换。
一般来说,v-if有更高的切换开销,而v-show有更高的初始渲染开销。因此,如果需要非常频繁地切换,则使用v-show较好,如果在运行时条件很少改变,则使用v-if较好。
七、v-bind
绑定属性,不多说了,注意冒号后面跟标签的属性,属性后面的等号指向数据,它可以简写为 :class, :href。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../statics/vue.min.js"></script> <style> .active { height: 100px; 100px; background-color: red; } </style> </head> <body> <div id="app" > <!--<div v-bind:class="{active: isActive}">xxxx</div>--> <!--<div v-bind:class="[isActive]">Hello World!</div>--> <div :class="[isActive]">Hello World!</div> </div> <script> new Vue({ el: "#app", data: { isActive: 'active', } }) </script> </body> </html>
八、v-model
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../statics/vue.min.js"></script> <style> .active { height: 100px; 100px; background-color: red; } </style> </head> <body> <div id="app" > <input type="text" v-model="name"/> <input type="checkbox" value="男" v-model="gender"/> <input type="checkbox" value="女" v-model="gender"/> <p>请选择你的女朋友</p> <select id="" name="" v-model="girlFriends"> <option>alex</option> <option>peiqi</option> <option>egon</option> </select> <hr> {{ name }} {{ gender }} {{ girlFriends}} </div> <script> new Vue({ el: "#app", data: { name: 'pizza', gender: [], girlFriends: [] } }) </script> </body> </html>
九、v-on
另一个非常重要的指令是v-on,使用v-on我们可以在标签上面绑定事件,注意我们新建的vue实例app01中多了一个属性,methods,在methods中,是我们具体事件的实现方式。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../statics/vue.min.js"></script> <style> .box { color: green; } </style> </head> <body> <div id="app" > <p v-bind:class="{box: isShow}">alex</p> <button v-on:click="doAlex">点击让Alex变绿</button> </div> <script> new Vue({ el: "#app", data: { name: 'pizza', gender: [], girlFriends: [], isShow: false }, methods: { doAlex: function () { this.isShow = true; }, doPeiqi: function () { } } }) </script> </body> </html>
十、指令修饰符
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../statics/vue.min.js"></script> <style> .box { color: green; } </style> </head> <body> <div id="app" > <table border="1"> <thead> <tr> <th>学科</th> <th>成绩</th> </tr> </thead> <tbody> <tr> <td>Python</td> <td><input type="text" v-model.number="python"/></td> </tr> <tr> <td>Linux</td> <td><input type="text" v-model.lazy="linux"/></td> </tr> <tr> <td>Vue</td> <td><input type="text" v-model.trim="vue"/></td> </tr> </tbody> </table> <hr> {{ python }} {{ linux }} {{ vue }} </div> <script> const vm = new Vue({ el: "#app", data: { python: 60, linux: 60, vue: 60 } }) </script> </body> </html>
十一、计算属性
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../statics/vue.min.js"></script> <style> .box { color: green; } </style> </head> <body> <div id="app" > <table border="1"> <thead> <tr> <th>学科</th> <th>成绩</th> </tr> </thead> <tbody> <tr> <td>Python</td> <td><input type="text" v-model.number="python"/></td> </tr> <tr> <td>Linux</td> <td><input type="text" v-model.lazy="linux"/></td> </tr> <tr> <td>Vue</td> <td><input type="text" v-model.trim="vue"/></td> </tr> <tr> <td>总成绩</td> <td>{{ sumScore }}</td> </tr> </tbody> </table> <hr> {{ python }} {{ linux }} {{ vue }} {{ sumScore }} <h1>{{ greeting }}</h1> <h1>{{ reversedGreeting }}</h1> <input type="text" v-model="name"/> </div> <script> const vm = new Vue({ el: "#app", data: { python: 60, linux: 60, vue: 60, greeting: "hello vue!", name: 'alex', }, methods: { }, computed: { sumScore: function () { console.log(1); return this.python + this.linux + this.vue; }, reversedGreeting: function () { return this.greeting.split('').reverse().join(''); }, }, watch: { name: function () { console.log(2); console.log("hahahah"); } } }) </script> </body> </html>
十二、自定义属性
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../statics/vue.min.js"></script> <style> .box { background-color: red; 100px; height: 100px; } </style> </head> <body> <div id="app" > <div v-pos.bottom.right="post" v-bind:class="{ box: isBox }">alex is big sb.</div> </div> <script> Vue.directive("pos", function (el, bindding) { console.log("el: ", el); console.log("bindding: ", bindding); if (bindding.value) { el.style.position = 'fixed'; for (let key in bindding.modifiers) { el.style[key] = 0; } } }); const vm = new Vue({ el: "#app", data: { isBox: true, post: false, }, methods: { }, computed: { }, watch: { } }) </script> </body> </html>
十三、获取DOM元素
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../statics/vue.min.js"></script> <style> .box { color: green; } </style> </head> <body> <div id="app" > <p ref="myAlex">alex</p> <button v-on:click="doAlex">点击让Alex变绿</button> </div> <script> const VM = new Vue({ el: "#app", data: { name: 'pizza', gender: [], girlFriends: [], isShow: false }, methods: { doAlex: function () { this.$refs.myAlex.style.color = 'green'; }, doPeiqi: function () { } } }) </script> </body> </html>