模板语法 mustache语法 双大括号语法

指令

Vue
指令
组件

  • 作用: 是用来操作DOM的,指令就是绑定在DOM身上的一个属性,这个属性具备一定的功能,这个功能用来操作DOM
  • 以后我们不在像以前一样,先获取DOM,在操作了,我们现在可以直接使用指令来操作DOM
  • 这个指令需要模板语法的支持,所以我们采用jsx语法糖

模板语法

模板语法支持性还是很高的,数据类型都是支持的,但是不支持 输出语法 ( console.log alert )

mustach例:

<!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>
</head>

<body>
    <div id="app">
        <p> str: {{ str }} </p>
        <p> num: {{ num }} </p>
        <p> bool: {{ bool?'true':'false' }} </p>
        <p> bool: {{ bool&&'true'||'false' }} </p>
        <p> null: {{ nul&&'null'||'unnull' }} </p>
        <p> undefined: {{ und && 'undefined' || '520' }} </p>
        <p> arr: {{ arr[0] }} </p>
        <p> obj: {{ obj.name }} </p>
        <!-- <p> console.log  {{ console.log( '1902' ) }} </p> -->
        <!-- <p> alert: {{ alert( '千锋教育' ) }} </p> -->

        <p> function : {{ ( function(){ return '千锋教育' })() }} </p>

        <p> msg内容反向:{{ msg.split('').reverse().join('') }} </p>

    </div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
    /* 
                    {{}} 语法
                  */
    new Vue({
        el: '#app',
        data: {
            msg: 'hello vue.js',
            str: 'str',
            num: 100,
            bool: true,
            nul: null,
            und: undefined,
            arr: [1, 2, 3, 4],
            obj: {
                name: 'yyb'
            }
        }
    })
</script>

</html>

//str: str
num: 100
bool: true
bool: true
null: unnull
undefined: 520
arr:1
obj:yyb
function : 千锋教育
msg内容反向:sj.euv olleh

指令

  1. 格式:

v-xxx = "mustache语法"

v-xxx = "msg"

v-xxx = "{{msg}}" ×

  1. v-html 将一个数据展示在一个DOM内容中, innerHTML( html属性 )
  • 防止脚本攻击 xss CSRF
  1. v-bind 单项数据绑定
  • 使用技巧: 凡是 DOM 的属性要和数据进行绑定,那么我们就可以使用 v-bind
  • 格式: v-bind:DOMattr = "data"
  • 简写: :DOMattr = "data"
  1. v-text 非转义输出

例:

<!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>
</head>

<body>
    <div id="app">
        <p> {{ msg }} </p>
        <h3> v-html </h3>
        <p v-html="msg"> </p>

        <h3> v-bind 单项数据绑定 </h3>
        <input type="text" v-bind:value="val" />
        <input type="text" :value="val" />
        <img :src="img" alt="">

        <h3> v-text </h3>
        <p v-text="msg"></p>
        <h3> v-html vs v-text </h3>
        <div v-html="a"></div>
        <div v-text="a"></div>
    </div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            msg: 'hello Vue.js',
            val: '千锋教育',
            img: 'https://www.baidu.com/img/dong_96c3c31cae66e61ed02644d732fcd5f8.gif',
            a: " <strong> 千锋教育 </strong> "
        }
    })
</script>

</html>
  1. class vs style
  2. class
    - object
      <div :class = "{[size]:true,[color]: true,[box]: true}"></div>
      <div :class = "{[size]: 5>2?true:false,[color]: true,[box]: true}"></div>
    
    • arr
        <div :class = "[size,box,color]"></div>
        <div :class = "[class_flag?size:'',box,color]"></div>
      
    • 兵哥推荐你使用arr形式

例:

<!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>
        .box_size {
            width: 100px;
            height: 100px;
        }
        
        .box_color {
            background: red;
            color: white;
            margin: 10px;
        }
    </style>
</head>

<body>
    <div id="app">
        <h3> 类名 </h3>
        <div class="box box_size box_color"> 千锋教育 </div>
        <h3> vue中类名的使用有两种形式 </h3>
        <hr>
        <h3> vue - class - 对象形式 </h3>
        <div :class="{[size]:true,[color]: true}"></div>
        <div :class="{[size]: 5>2?true:false,[color]: true}"></div>
        <hr>
        <h3> vue -class - arr </h3>
        <div :class="[size,color]"></div>
        <div :class="[class_flag?size:'',color]"></div>
    </div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            msg: '千锋教育',
            size: 'box_size',
            color: 'box_color',
            class_flag: true
        }
    })
</script>

</html>
  1. style
  • object
       <div :style = "{width:'100px',height: '100px',background: 'blue'}"></div>
       <div :style = "style"></div>
  • arr
    <div :style = "[style,border]"></div>
  1. 条件渲染 v-if && v-show

1.v-if 如果值为false,那么绑定的DOM就会被销毁

2.v-if 操作的是一个DOM的生成和销毁

3.如果v-if的初始值时false,那么绑定元素是否会渲染呢?

v-if如果是false,那么这个DOM元素是不会渲染的

例: v-if

<!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>
  <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
</head>
<body>
  <div id="app">
    <h3> v-if 单路</h3>
      <p v-if = "flag"> 单路分支 </p>
    <h3> v-if 双路 </h3>
      <p v-if = "flag"> 双路1 </p>
      <p v-else> 双路2 </p>
    <h3> v-if 多路 </h3>
      <p v-if = "type === 'A' "> A </p>
      <p v-else-if = " type === 'B'"> B </p>
      <p v-else> C </p>
  </div>
</body>
<script>
  new Vue({
    el: '#app',
    data: {
      msg: 'hello  下午到了',
      flag: false,
      type: 'A'
    }
  })
</script>
</html>

//  v-if 单路
    v-if 双路
    双路2
    v-if 多路
    A

例:v-show

  1. v-show 操作的是一个DOM的dispay样式属性
  2. 如果v-show的初始值是false,那么这个绑定的DOM元素是否会渲染呢?

v-show不管值是什么,它都会渲染出来

<!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>
  <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
</head>
<body>
  <div id="app">
    <h3> v-show </h3>
    <p v-show = "flag"> 千锋教育 </p>

    <template v-if = 'flag'>
      <div class="box" >
        <ul>
          <li>1</li>
          <li>2</li>
          <li>3</li>
          <li>4</li>
          <li>5</li>
          <li>6</li>
          <li>7</li>
          <li>8</li>
          <li>9</li>
          <li>10</li>
        </ul>
      </div>
    </template>
  </div>
  <template>
    123
  </template>

</body>
<script>
  /* 
    1. v-show 操作的是一个DOM的dispay样式属性
    2. 如果v-show的初始值是false,那么这个绑定的DOM元素是否会渲染呢?
      v-show不管值是什么,它都会渲染出来

   */
  new Vue({
    el: '#app',
    data: {
      msg: 'hello vue.js',
      flag: false
    }
  })
</script>
</html>
  1. 条件渲染 v-if && v-show

条件渲染有两个指令, 一个是 v-if , 另外一个是 v-show

  • v-if 有三种使用形式

    • 单路分支
    • 双路分支
    • 多路分支
  • v-show

  • v-if vs v-show
    一般来说,v-if 有更高的切换开销,而 v-show 有更高的初始渲染开销。因此,如果需要非常频繁地切换,则使用 v-show 较好;如果在运行时条件很少改变,则使用 v-if 较好。

  • template
    template标签如果放在模板的范围内使用,那么将来不会被解析渲染

  1. 列表渲染
    v-for 是用来做列表渲染的

    • 格式
      v-for = " xxx in(of) data "
      举例:
      v-for = " item in(of) todos "

    • 带参数的格式
      v-for = " (item,index) in todos "

    • key( 留一部分 )

    • 每次列表循环的后面都要绑定一个key,是为了进行DOM的唯一标识,这样就不会让vue因为惰性而影响列表的正常渲染

    • 理想的key是使用数据中的id

例:

<!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>
    <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
</head>

<body>
    <div id="app">
        <h3> num </h3>
        <ul>
            <li v-for=" n in num "> {{ n }} </li>
        </ul>
        <hr>
        <h3> string </h3>
        <ul>
            <li v-for=" s of str"> {{ s }} </li>
        </ul>
        <hr>
        <h3> arr </h3>
        <ul>
            <li v-for=" item in arr "> {{ item }} </li>
        </ul>
        <h3> arr - v-for 带参数的 </h3>
        <ul>
            <li v-for="( item , index) in arr ">
                <p> item -- {{ item }} </p>
                <p> index ---{{ index }} </p>
            </li>
        </ul>
        <hr>
        <h3> object </h3>
        <ul>
            <li v-for=" item in obj "> {{ item }} </li>
        </ul>
        <h3> object-v-for 带参数 </h3>
        <ul>
            <li v-for=" (item,key) in obj ">
                <p> item -- {{ item }} </p>
                <p> key -- {{ key }} </p>
            </li>
        </ul>
        <h3> object-v-for 带三个参数 </h3>
        <ul>
            <li v-for=" (item,key,index) in obj ">
                <p> item -- {{ item }} </p>
                <p> key -- {{ key }} </p>
                <p> index -- {{ index }} </p>
            </li>
        </ul>
        <hr>
        <h3> json </h3>
        <button v-on:click="add"> 添加 </button>
        <button v-on:click="notChange"> 不能检测的 </button>
        <button v-on:click="clear"> 清空一个数组 </button>
        <ul>
            <li v-for="item in json">
                <ul>
                    <li v-for="(ele,index) in item" :key="index">
                        {{ ele }}
                    </li>
                </ul>
            </li>
        </ul>
        <hr>
        <h3> 新数组 computed - filter </h3>
        <ul>
            <li v-for=" item in newJson" :key="item.id">
                {{ item.text }}
            </li>
        </ul>
    </div>
</body>
<script>
    new Vue({
        el: '#app',
        data: {
            msg: 'hello vue.js',
            num: 10,
            str: 'I hate you ~~',
            arr: [1, 2, 3, 4],
            obj: {
                id: 1,
                name: 'yyb',
                age: 18
            },
            json: [
                'aa', {
                    id: 1,
                    text: '睡觉'
                }, {
                    id: 2,
                    text: '敲代码'
                }
            ]
        },
        methods: {
            //这里面存放的都是事件的处理程序
            add() {
                this.json.push({
                    id: 3,
                    text: '打篮球'
                })
            },

            notChange() {
                // this.json[0] = '做作业'
                // Vue.set(this.json,0,'做作业')
                this.$set(this.json, 0, '做作业')
            },

            clear() {
                // this.json.length = 0
                this.json.splice(0, this.json.length)
            }
        },
        computed: { // 计算属性
            // 这里存放的也是方法,但是这个方法是有返回值的,并且方法名还可以当做一个变量(相当于直接在data里面定义的数据)来使用
            newJson() {
                return this.json.filter(item => {
                    return item.id > 1
                })
            }
        }
    })
</script>

</html>

事件的修饰符

格式: v-on:click.xxx = 'handler'
xxx指的是修饰符的名称

问题: 修饰符使用有什么好处?

举例: 事件冒泡案例

分析: 发现 e.stopPropagation() 在每一个方法中都要使用,这个时候发现代码很冗余

解决: 事件修饰符

例:

<!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>
  <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
  <style>
    .big{
      width: 300px;height: 300px;
      background: red;
    }
    .middle{
      width: 200px;height: 200px;
      background: purple;
    }
    .small{
      width: 100px;height: 100px;
      background: blue;
    }
  </style>
</head>
<body>
  <div id="app">
      <div class="big" @click.stop = "bigHandler">
        <div class="middle" @click.stop = "middleHandler">
          <div class="small" @click.stop = "smallHandler"></div>
        </div>
      </div>
      <hr>
      <div class="big" @click.self = "bigHandler">
        <div class="middle" @click.self = "middleHandler">
          <div class="small" @click.self = "smallHandler"></div>
        </div>
      </div>
  </div>
</body>
<script>
  new Vue({
    el: '#app',
    methods: {
      bigHandler (e) {
        // e.stopPropagation()
        alert('big')
      },
      middleHandler (e) {
        // e.stopPropagation()
        alert('middle')
      },
      smallHandler (e) {
        // e.stopPropagation()
        alert( 'small' )
      }
    }
  })
</script>
</html>

按键修饰符
案例: 敲击键盘的enter键,将用户输入的数据拿到手?

事件的书写
先写方法,然后在绑定DOM元素身上

// box.onclick = function(){}

原因:我们发现当我们需要书写很多下面类似代码的时候,发现代码在重复,我们希望代码能复用或是简写
解决: 它将这部分代码封装在自己的 Vue.js中,然后我们来调用这个功能,我们通过按键修饰符来使用

自定义按键修饰符键盘码

Vue.config.keyCodes.p = 80

例:

<!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>
  <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
</head>
<body>
  <div id="app">
    <input type="text" @keyup = "get_user_input">
    <h3> 按键修饰符 </h3>
    <input type="text" @keyup.enter = "get_user_shuru">
    <input type="text" @keyup.13 = "get_user_shuru">
    <input type="text" @keyup.s = "get_user_shuru">
  </div>        
</body>
<script>

  new Vue({
    el: '#app',
    methods: {
      get_user_input ( e ) {
        const keyCode = e.keyCode
        const target = e.target 
        if( keyCode === 13 ){
            console.log( target.value )
        }
      },
      get_user_shuru ( e  ) { // 用户输入获取value的方法
        console.log( e.target.value )
      }
    }
  })
</script>
</html>

双向数据绑定:
VM改变,V响应
V改变, VM也会响应

双向数据绑定原理/数据驱动/深入响应式原理:
vue是通过数据劫持和事件的发布订阅来进行数据驱动的,当我们在data选项中定义数据后,vue会通过observer(观察者)
监听data选项,将data选项中的所有key通过es5的Object.definedPropty进行getter和setter设置,当
数据绑定在DOM上是,就会触发getter,给DOM设置初始值,当我们在V(视图中)输入内容时,就会触发setter
,就可以获得最新的值,通过watch(监听)通知Vue进行V(视图)重新渲染

vue中通过 v-model 来进行双向数据绑定

v-model只用于表单元素

v-model默认绑定 value属性

格式:

购物车数量增减例:

<!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>
  <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>

</head>
<body>
  <div id="app">
    <input type="text" v-model.trim = "msg">
    <input type="text" v-model.number = "num">
    <h3> {{ msg }} </h3>
    <h3> {{num}} </h3>

    <hr>
    <h3> 购物车加减 </h3>
    <div>
      <button @click = "decrement"> - </button>
      <input type="text" v-model = 'number'>
      <button @click = "increment"> + </button>
    </div>
  </div>
</body>
<script>
  new Vue({
    el: '#app',
    data: {
      msg: '  hello 1902   ',
      num: 0.898098908,
      number: 1
    },
    methods: {
      increment () { //加
        this.number ++ 
      },
      decrement () { // 减
        this.number --
      }
    }
  })
</script>
</html>

数据绑定案例:

<!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>
  <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>

</head>
<body>
  <div id="app">
    <input type="text" :value = "msg" @input = "getValue">
    <h3> {{ msg }} </h3>  
  </div>
</body>
<script>
  new Vue({
    el: '#app',
    data: {
      msg: '请输入'
    },
    methods: {
      getValue ( e ) {
        var val = e.target.value 
        this.msg = val
      }
    }
  })
</script>
</html>

model修饰符例:

<!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>
  <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>

</head>
<body>
  <div id="app">
    <input type="text" v-model.trim = "msg" @blur = "clear_trim">
    <h3> {{ msg }} </h3>  
  </div>
</body>
<script>
  new Vue({
    el: '#app',
    data: {
      msg: ' 请输入 '
    },
    methods: {
      clear_trim (  ) {
        this.msg = this.msg.trim()
      }
    }
  })
</script>
</html>

数据的更新检测
a.使用以下方法操作数组,可以检测变动
push()pop()shift()unshift()splice()sort()reverse()
b.filter(),concat()和slice(),map(),新数组替换旧数组
c.不能检测以下变动的数组
vm.items[indexOfItem]=newValue
解决
(1)Vue.set(example1.items,indexOfItem,newValue)
vm.items.length = 0

解决(1)splice

  • methods 方法

  • 事件的添加使用的是 v-on:eventType = '事件处理程序'

    • 事件处理程序往options里面的methods配置项中书写
      <button v-on:click = "add"> + </button>
    
      new Vue({
        el: '#app',
        data: {
          arr: [1,2,3,4]
        },
        methods: {
          add () {
            this.arr.push(5)
          }
        }
      })
    
  • computed 计算属性

      1. 计算属性中存放的也是方法
      1. 计算属性的方法中必须要有返回值
      1. 计算属性的方法名可以像data选项中定义的数据一样使用

事件

  1. 指令v-on
  2. 格式
    v-on:eventType = "事件处理程序名称"
  3. 简写
    @eventType = '事件处理程序名称'