Vue官网使用指南
https://cn.vuejs.org/
https://www.bilibili.com/video/BV1Zy4y1K7SH/?p=3
安装和启动
| 12
 3
 4
 5
 6
 7
 8
 
 | $ npm install -g @vue/cli-service-global
 $ vue serve
 
 
 
 
 
 
 | 
srcipt 标签引用版本
- 开发版本: 包含完整的警告和调试模式。=>   vue.js
- 生产版本: 删除了警告,压缩。       =>   vue.min.js
模板语法
- 插值语法 - {{xxx}}
 
- 指令语法 - 
- v-bind:href="xxx"简写- :href="xxx"
 
数据绑定
- v-bind  
- v-model
- data <==> 页面(双向)
- 只能表单类元素,默认收集value值
 
el两种写法
| 12
 3
 4
 5
 
 | const v = new Vue({el:'#root',
 })
 
 v.$mount('#root')
 
 | 
data两种写法
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | new Vue({el:'#root',
 
 
 data: {
 ...
 }
 
 
 
 data() {
 return{
 ...
 }
 }
 })
 
 | 
Object.defineProperty
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 
 | Object.defineProperty(person,'age',{
 
 
 
 
 
 get(){
 return number
 },
 
 
 set(value){
 number = value
 }
 
 })
 
 | 
数据代理
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 
 | let data = {x:100}let vm = {y:200}
 
 Object.defineProperty(vm,'x',{
 get(){
 return data.x
 },
 set(value){
 data.x = value
 }
 })
 
 {{vm.x}}
 
 | 
事件处理
v-on:xxx === @xxx
xxx 为事件名
| 1
 | <button @click="fn($event, data)">btn</button>
 | 
事件修饰符
- prevent:阻止默认事件 (常用)
- stop:阻止事件冒泡 (常用)
- once:事件只触发一次 (常用)
- capture:使用事件的捕获模式 - 父元素不触发子元素事件
- self:只有- event.target是当前操作的元素时才触发事件;
- passive:事件的默认行为立即执行,无需等待事件回调执行完毕;| 12
 
 | <a href="https://tenyding.cn" @click.prevent.stop="showInfo">点我提示信息</a><ul @wheel.passive="demo" class="list"></ul>
 
 |  
 
键盘事件
| 按键 | 事件名 |  | 按键 | 事件名 | 
| 回车 | enter |  | 删除 | delete (捕获“删除”和“退格”键) | 
| 退出 | esc |  | 空格 | space | 
| 上 | up |  | 下 | down | 
| 左 | left |  | 右 | right | 
注:
| 1
 | Vue.config.keyCodes.huiche = 13 
 | 
| 12
 3
 4
 5
 
 | <input type="text" placeholder="按下ctrl+y触发showInfo" @keyup.ctrl.y="showInfo"><input type="text" placeholder="按下CapsLock(大小写切换)触发showInfo" @keyup.caps-lock="showInfo">
 <input type="text" placeholder="按下回车触发showInfo" @keyup.13="showInfo">
 <input type="text" placeholder="按下回车触发showInfo" @keyup.enter="showInfo">
 <input type="text" placeholder="按下回车触发showInfo" @keydown.huiche="showInfo">
 
 | 
计算属性
- 定义:要用的属性不存在
- 原理:利用 Object.defineproperty的getter和setter
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 
 | computed:{
 fullName:{
 
 get(){
 return this.firstName + '-' + this.lastName
 },
 
 set(value){
 this.firstName = value.split('-')[0]
 this.lastName = value.split('-')[1]
 }
 }
 
 fullName(){
 return this.firstName + '-' + this.lastName
 }
 }
 
 | 
监视属性
前提:xxx 需存在
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 | watch: {
 
 
 xxx:{
 immediate: true,
 deep: true,
 
 handler(newValue, oldValue){
 ...
 }
 }
 
 
 xxx(newValue, oldValue){
 ...
 }
 }
 
 
 vm.$watch('xxx',(newValue,oldValue)=>{
 ...
 })
 
 | 
绑定样式
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | data {mood: 'style1',
 
 classObj:{
 style1: true,
 style2: false
 },
 
 styleObj:{
 fontSize: '40px',
 color:'red',
 },
 
 styleArr:[{
 fontSize: '40px',
 color:'blue',
 }, {
 backgroundColor:'gray'
 }]
 }
 
 | 
| 12
 3
 4
 5
 6
 
 | <div :class="mood" @click="changeMood"></div><div :class="['style1','style2','style3']"></div>
 <div :class="classObj"></div>
 
 <div :style="styleObj"></div>
 <div :style="styleArr"></div>
 
 | 
条件渲染
| 12
 3
 4
 5
 6
 7
 
 | <div v-show="true">hello</div>
 <template v-if="n > 0">
 <div v-if="n === 1">Angular</div>
 <div v-else-if="n === 2">React</div>
 <div v-else>Vue</div>
 </template>
 
 | 
列表渲染