初始Vue
1、Vue是什么?
- 一套用语和构建用户界面的渐进式JavaScript框架
2、谁开发的?
3、Vue的特点
- 采用组件化模式,提高代码复用率、且让代码更好维护
- 声明式编码,让编码人员无需直接操作DOM,提高开发效率
- 使用虚拟DOM+优秀的Diff算法,尽量复用DOM节点
搭建Vue开发环境
Vue2.0版本CDN引入
1 2
| <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.13/vue.js"></script>
|
Hello案例
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
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>初始Vue</title> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.13/vue.js"></script> </head>
<body>
<div id="root"> <h1>Hello,{{name}}</h1> </div> <script type="text/javascript"> Vue.config.productionTip = false
new Vue({ el: '#root', data: { name: '上师大' } });
</script> </body>
</html>
|
容器和Vue实例的对应关系
容器与实例多对一(错)
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
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>初始Vue</title> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.13/vue.js"></script> </head>
<body> <div class="root"> <h1>Hello,{{name}}</h1> </div> <div class="root"> <h1>Hello,{{name}}</h1> </div> <script type="text/javascript"> Vue.config.productionTip = false new Vue({ el: '.root', data: { name: '上师大' } });
</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
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>初始Vue</title> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.13/vue.js"></script> </head>
<body> <div id="root"> <h1>Hello,{{name}}, {{address}}</h1> </div>
<script type="text/javascript"> Vue.config.productionTip = false new Vue({ el: '#root', data: { name: '上师大' } }); new Vue({ el: '#root', data: { address: '上海徐汇' } });
</script> </body>
</html>
|
结论:容器和Vue实例只能是一对一的关系
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
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>初始Vue</title> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.13/vue.js"></script> </head>
<body>
<div id="root"> <h1>Hello,{{name}}, {{address}}, {{Date.now()}}</h1> </div> <div id="root2"> <h1>Hello,{{name}}, {{address.toUpperCase()}}</h1> </div>
<script> Vue.config.productionTip = false new Vue({ el: '#root', data: { name: '上师大', address: '上海徐汇' } }); new Vue({ el: '#root2', data: { name: 'ShangHaiNormalUniversity', address: 'ShangHaiXuHui' } });
</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
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>Vue模板语法</title> <script src="../js/vue.js"></script> </head>
<body>
<div id="root"> <h1>插值语法</h1> <h3>你好,{{name}}</h3> <br /> <h1>指令语法</h1> <a v-bind:href="school.url.toUpperCase()">点击我去{{school.name}}博客学习1</a> <a :href="school.url">点击我去{{school.name}}博客学习2</a> </div> <script type="text/javascript"> Vue.config.productionTip = false
new Vue({ el: "#root", data: { name: 'jack', school: { name: '聂千龙', url: 'https://www.nieqianlong.cn' } } }); </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
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>数据绑定</title> <script src="../js/vue.js"></script> </head>
<body>
<div id="root">
单向数据绑定:<input type="text" :value="name"><br /> 双向数据绑定:<input type="text" v-model="name"> <h2 v-bind:x="name">你好啊!</h2> </div> <script type="text/javascript"> Vue.config.productionTip = false
new Vue({ el: "#root", data: { name: '聂千龙' } }); </script> </body>
</html>
|
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 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
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>数据绑定</title> <script src="../js/vue.js"></script> </head>
<body>
<div id="root"> <h1>你好,{{name}}</h1> </div> </body> <script type="text/javascript"> Vue.config.productionTip = false
new Vue({ el: "#root",
data() { console.log('@@@', this) return { name: "上师大" } } }); </script>
</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
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>数据绑定</title> <script src="../js/vue.js"></script> </head>
<body>
<div id="root"> <h1>学校名称:{{name}}</h1> <h1>学校地址:{{address}}</h1> <h1>测试一下1:{{1+1}}</h1> <h1>测试一下2:{{$options}}</h1> <h1>测试一下3:{{$emit}}</h1> <h1>测试一下4:{{_c}}</h1> </div> </body> <script type="text/javascript"> Vue.config.productionTip = false const vm = new Vue({ el: "#root", data() { return { name: '上师大', address: '上海徐汇' } } }); console.log(vm) </script>
</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
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>回顾Object.defineProperty方法</title> <script src="../js/vue.js"></script> </head>
<body>
</body> <script type="text/javascript"> let number = 170; let person = { name: '聂千龙', gender: '男', }
Object.defineProperty(person, 'age', { value: 18, enumerable: true, writeable: true, configurable: true }) Object.defineProperty(person, 'height', { get() { console.log('有人读取height属性了') return number }, set(value) { console.log('有人修改了height属性,且值是', value) number = value; } })
console.log(person) </script>
</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
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>何为数据代理</title> <script src="../js/vue.js"></script> </head>
<body> <script type="text/javascript"> let obj = { x: 100 } let obj2 = { y: 200 }
Object.defineProperty(obj2, 'x', { get() { return obj.x }, set(value) { obj.x = value } }) </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
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>Vue中的数据代理</title> <script src="../js/vue.js"></script> </head>
<body>
<div id="root"> <h2>学校名称:{{name}}</h2> <h2>学校地址:{{address}}</h2> </div> </body> <script type="text/javascript"> Vue.config.productionTip = false
let data = { name: '上师大', address: '上海徐汇' }
const vm = new Vue({ el: "#root", data }); </script>
</html>
|
vm对_data做了数据代理,使得vm.name和vm.address对应的便是_data中的name和address,Vue还对_data做了升级(数据劫持)以便于data中的数据改变后能使得使用到data中数据的地方能跟随变化,并不是在_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 40 41 42 43 44 45 46 47 48 49 50 51
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>事件处理的基本使用</title> <script src="../js/vue.js"></script> </head>
<body>
<div id="root"> <h2>欢迎来到{{name}}学习</h2> <button v-on:click="showInfo1">点我提示信息1(不传参)</button> <button @click="showInfo2(66,$event)">点我提示信息2(传参)</button> </div> </body> <script type="text/javascript"> Vue.config.productionTip = false const vm = new Vue({ el: "#root", data() { return { name: '上师大' } }, methods: { showInfo1(event) { alert('同学你好!') }, showInfo2(number, event) { console.log(number, event) alert('同学你好!!') } } }); </script>
</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 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
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>事件修饰符</title> <script src="../js/vue.js"></script> <style> * { margin-top: 20px; }
.demo1 { height: 50px; background-color: skyblue; }
.box1 { padding: 5px; background-color: skyblue; }
.box2 { padding: 5px; background-color: orange; }
.list { width: 200px; height: 200px; background-color: peru; overflow: auto; }
li { height: 100px; } </style> </head>
<body>
<div id="root"> <h2>欢迎来到{{name}}学习</h2> <a href="http://www.nieqianlong.cn" @click.prevent="showInfo">点我查看笔记</a> <div class="demo1" @click="showInfo"> <button @click.stop="showInfo">点我提示信息</button> </div> <button @click.once="showInfo">点我提示信息</button> <div class="box1" @click.capture="showMsg(1)"> div1 <div class="box2" @click="showMsg(2)"> div2 </div> </div> <div class="demo1" @click.self="showInfo"> <button @click="showInfo">点我提示信息</button> </div>
<ul @wheel.passive="demo" class="list"> <li>1</li> <li>2</li> <li>3</li> <li>4</li> </ul>
</div> </body> <script type="text/javascript"> Vue.config.productionTip = false
new Vue({ el: "#root", data() { return { name: '上师大' } }, methods: { showInfo(e) { alert('同学你好!') }, showMsg(msg) { console.log(msg) }, demo() { for (let i = 0; i < 1000; i++) { console.log('#') } console.log('累坏了') } } }); </script>
</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 80
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>键盘事件</title> <script src="../js/vue.js"></script> </head>
<body>
<div id="root"> <h2>欢迎来到{{name}}学习</h2> <input type="text" placeholder="按下回车提示输入" @keyup.huiche="showInfo"> </div> </body> <script type="text/javascript"> Vue.config.productionTip = false Vue.config.keyCodes.huiche = 13
new Vue({ el: "#root", data() { return { name: '上师大' } }, methods: { showInfo(e) { console.log(e.target.value)
} } }); </script>
</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
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>姓名案例_插值语法实现</title> <script src="../js/vue.js"></script> </head>
<body> <div id="root"> 姓:<input type="text" v-model="firstName"><br /><br /> 名:<input type="text" v-model="lastName"><br /><br /> 全名:<span>{{firstName.slice(0,3)}}-{{lastName}}</span> </div> </body> <script type="text/javascript"> Vue.config.productionTip = false
new Vue({ el: "#root", data() { return { firstName: '张', lastName: '三' } } }); </script>
</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
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>姓名案例_method实现</title> <script src="../js/vue.js"></script> </head>
<body> <div id="root"> 姓:<input type="text" v-model="firstName"><br /><br /> 名:<input type="text" v-model="lastName"><br /><br /> 全名:<span>{{fullName()}}</span><br /><br /> 全名:<span>{{fullName()}}</span><br /><br /> 全名:<span>{{fullName()}}</span><br /><br /> 全名:<span>{{fullName()}}</span><br /><br /> 全名:<span>{{fullName()}}</span> </div> </body> <script type="text/javascript"> Vue.config.productionTip = false
new Vue({ el: "#root", data() { return { firstName: '张', lastName: '三' } }, methods: { fullName() { console.log('@-----') return this.firstName + '-' + this.lastName } } }); </script>
</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
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>姓名案例_计算属性实现</title> <script src="../js/vue.js"></script> </head>
<body>
<div id="root"> 姓:<input type="text" v-model="firstName"><br /><br /> 名:<input type="text" v-model="lastName"><br /><br /> 测试:<input type="text" v-model="x"><br /><br /> 全名:<span>{{fullName}}</span><br /><br /> 全名:<span>{{fullName}}</span><br /><br /> 全名:<span>{{fullName}}</span><br /><br /> 全名:<span>{{fullName}}</span> </div> </body> <script type="text/javascript"> Vue.config.productionTip = false
const vm = new Vue({ el: "#root", data() { return { firstName: '张', lastName: '三', x: '你好' } }, computed: { fullName: { get() { console.log('get被调用') return this.firstName + '-' + this.lastName }, set(value) { console.log('set', value) const arr = value.split('-') this.firstName = arr[0] this.lastName = arr[1] } } } }); </script>
</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
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>姓名案例_计算属性简写</title> <script src="../js/vue.js"></script> </head>
<body> <div id="root"> 姓:<input type="text" v-model="firstName"><br /><br /> 名:<input type="text" v-model="lastName"><br /><br /> 全名:<span>{{fullName}}</span><br /><br /> </div> </body> <script type="text/javascript"> Vue.config.productionTip = false
const vm = new Vue({ el: "#root", data() { return { firstName: '张', lastName: '三', } },
computed: { fullName() { console.log('get被调用') return this.firstName + '-' + this.lastName } } }); </script>
</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
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>监视属性</title> <script src="../js/vue.js"></script> </head>
<body> <div id="root"> <h2>今天天气很{{info}}</h2> <button @click="changeWeather">切换天气</button> </div> </body> <script type="text/javascript"> Vue.config.productionTip = false
const vm = new Vue({ el: "#root", data() { return { isHot: true } }, computed: { info() { return this.isHot ? '炎热' : '凉爽' } }, methods: { changeWeather() { this.isHot = !this.isHot } }, }); </script>
</html>
|