VUE基础
官方文档 https://cn.vuejs.org/v2/guide/
第一个vue程序
- 导入开发版本的vue.js
- 创建vue实例对象,设置el属性和data属性
- 使用模板语法把数据渲染到页面上
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
| <!DOCTYPE html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Vue基础</title> </head>
<body> <div id="app"> {{ message }} </div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> var app = new Vue({ el:"#app", data:{ message:" 你好 小ddddddd黑! " } }) </script> </body>
</html>
|
el:挂载点
Vue实例的作用范围是什么呢?
Vue会管理el选项命中的元素及其内部的后代元素
是否可以使用其他的选择器?
可以使用其他的选择器,但是建议使用ID选择器
是否可以设置其他的dom元素呢?
可以使用其他的双标签,不能使用HTML和BODY
data:数据对象
Vue中用到的数据定义在data中
data中可以写复杂类型的数据
渲染复杂类型数据时,遵守js的语法即可
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
| <!DOCTYPE html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>data:数据对象</title> </head>
<body> <div id="app"> {{ message }} <h2> {{ school.name }} {{ school.mobile }}</h2> <ul> <li>{{ campus[0] }}</li> <li>{{ campus[3] }}</li> </ul> </div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> var app = new Vue({ el:"#app", data:{ message:"你好 小黑!", school:{ name:"黑马程序员", mobile:"400-618-9090" }, campus:["北京校区","上海校区","广州校区","深圳校区"] } }) </script> </body>
</html>
|

vue指令
v-text
设置标签的文本值(textContent)
v-text指令的作用是:设置标签的内容(textContent)
默认写法会替换全部内容,使用差值表达式{{}}
可以替换指定内容
内部支持写表达式
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
| <!DOCTYPE html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>v-text指令</title> </head>
<body> <div id="app"> <h2 v-text="message+'!'">深圳</h2> <h2 v-text="info+'!'">深圳</h2> <h2>{{ message +'!'}}深圳</h2> </div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> var app = new Vue({ el:"#app", data:{ message:"黑马程序员", info:"前端与移动教研部" } }) </script> </body>
</html>
|

v-html
v-html指令的作用是:设置元素的innerHTML
内容中有html结构会被解析为标签
v-text指令无论内容是什么,只会解析为文本
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
| <!DOCTYPE html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>v-html指令</title> </head>
<body> <div id="app"> <p v-html="content"></p> <p v-text="content"></p> </div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> var app = new Vue({ el:"#app", data:{ content:"<a href=www.baidu.com>黑马程序员</a>" } }) </script> </body>
</html>
|

v-on
v-on指令的作用是:为元素绑定事件
事件名不需要写on
指令可以简写为@
绑定的方法定义在methods属性中
方法内部通过this关键字可以访问定义在data中数据
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
| <!DOCTYPE html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>v-html指令</title> </head>
<body> <div id="app"> <input type="button" value="v-on指令" v-on:click ="doit"> <input type="button" value="v-on指令@" @click ="doit"> <input type="button" value="v-on指令双击" @dblclick ="doit"> <h2 @click="change">{{content}}</h2> </div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> var app = new Vue({ el:"#app", methods:{ doit:function(){ alert("哈哈哈哈") }, change:function(){ this.content+="你好!" } }, data:{ content:"黑马程序员" }
}) </script> </body>
</html>
|

案例:计数器

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
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Document</title> <style> #app { width: 480px; height: 100px; margin: 200px auto; } .input-num { margin-top: 20px; height: 100%; display: flex; border-radius: 10px; overflow: hidden; box-shadow: 0 0 4px black; } .input-num button { width: 150px; height: 100%; font-size: 40px; color: gray; cursor: pointer; border: none; outline: none; } .input-num span { height: 100%; font-size: 40px; flex: 1; text-align: center; line-height: 100px; } </style> </head> <body> <div id="app"> <div class="input-num"> <button @click="sub">-</button> <span>{{num}}</span> <button @click="add">+</button> </div> </div> </body> </html>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script> var app = new Vue({ el: "#app", data: { num: 1 }, methods: { add: function() { if(this.num>9){ alert("已经最大"); return; } this.num++; }, sub: function() { if(this.num<1){ alert("已经最小"); return; } console.log(this.num) this.num--; } } }); </script>
|
源码下载