美文网首页
Vue-(3)循环-计算-监听-样式绑定-事件处理

Vue-(3)循环-计算-监听-样式绑定-事件处理

作者: 物非0人非 | 来源:发表于2021-08-18 09:21 被阅读0次

    一:循环语句

    Vue.js 循环语句,循环使用 v-for 指令。
    v-for 指令需要以 site in sites形式的特殊语法,sites是源数据数组并且 site 是数组元素迭代的别名。
    v-for可以绑定数据到数组来渲染一个列表:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    </head>
    <body>
    <div id="app">
      <ol>
        <li v-for="site in sites">
          {{ site.name }}
        </li>
      </ol>
    </div>
    
    <script>
    new Vue({
      el: '#app',
      data: {
        sites: [
          { name: 'Runoob' },
          { name: 'Google' },
          { name: 'Taobao' }
        ]
      }
    })
    </script>
    </body>
    </html>
    

    运行结果:

    1. Runoob
    2. Google
    3. Taobao
    

    v-for 迭代对象
    v-for 可以通过一个对象的属性来迭代数据:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    </head>
    <body>
    <div id="app">
      <ul>
        <li v-for="value in object">
        {{ value }}
        </li>
      </ul>
    </div>
    
    <script>
    new Vue({
      el: '#app',
      data: {
        object: {
          name: '菜鸟教程',
          url: 'http://www.runoob.com',
          slogan: '学的不仅是技术,更是梦想!'
        }
      }
    })
    </script>
    </body>
    </html>
    

    运行结果:

    菜鸟教程
    http://www.runoob.com
    学的不仅是技术,更是梦想!
    

    你也可以提供第二个的参数为键名:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    </head>
    <body>
    <div id="app">
      <ul>
        <li v-for="(value, key) in object">
        {{ key }} : {{ value }}
        </li>
      </ul>
    </div>
    
    <script>
    new Vue({
      el: '#app',
      data: {
        object: {
          name: '菜鸟教程',
          url: 'http://www.runoob.com',
          slogan: '学的不仅是技术,更是梦想!'
        }
      }
    })
    </script>
    </body>
    </html>
    

    运行结果:

    name : 菜鸟教程
    url : http://www.runoob.com
    slogan : 学的不仅是技术,更是梦想!
    

    第三个参数为索引:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    </head>
    <body>
    <div id="app">
      <ul>
        <li v-for="(value, key, index) in object">
         {{ index }}. {{ key }} : {{ value }}
        </li>
      </ul>
    </div>
    
    <script>
    new Vue({
      el: '#app',
      data: {
        object: {
          name: '菜鸟教程',
          url: 'http://www.runoob.com',
          slogan: '学的不仅是技术,更是梦想!'
        }
      }
    })
    </script>
    </body>
    </html>
    

    运行结果:

    0. name : 菜鸟教程
    1. url : http://www.runoob.com
    2. slogan : 学的不仅是技术,更是梦想!
    

    v-for 迭代整数
    v-for 也可以循环整数

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    </head>
    <body>
    <div id="app">
      <ul>
        <li v-for="n in 10">
         {{ n }}
        </li>
      </ul>
    </div>
    
    <script>
    new Vue({
      el: '#app'
    })
    </script>
    </body>
    </html>
    

    运行结果:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    

    二:监听属性

    Vue.js 监听属性 watch,我们可以通过 watch 来响应数据的变化

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
    </head>
    <body>
    <div id = "app">
     <p style = "font-size:25px;">计数器: {{ counter }}</p>
     <button @click = "counter++" style = "font-size:25px;">点我</button>
    </div>
    <script type = "text/javascript">
     var vm = new Vue({
        el: '#app',
        data: {
           counter: 1
        }
     });
     vm.$watch('counter', function(nval, oval) {
        alert('计数器值的变化 :' + oval + ' 变为 ' + nval + '!');
     });
    </script>
    </body>
    </html>
    

    运行结果:

    QQ20210817-102948-HD.gif

    以下实例进行千米与米之间的换算:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
        <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
    </head>
       <body>
          <div id = "computed_props">
             千米 : <input type = "text" v-model = "kilometers">
             米 : <input type = "text" v-model = "meters">
          </div>
           <p id="info"></p>
          <script type = "text/javascript">
             var vm = new Vue({
                el: '#computed_props',
                data: {
                   kilometers : 0,
                   meters:0
                },
                methods: {
                },
                computed :{
                },
                watch : {
                   kilometers:function(val) {
                      this.kilometers = val;
                      this.meters = this.kilometers * 1000
                   },
                   meters : function (val) {
                      this.kilometers = val/ 1000;
                      this.meters = val;
                   }
                }
             });
             // $watch 是一个实例方法
            vm.$watch('kilometers', function (newValue, oldValue) {
                // 这个回调将在 vm.kilometers 改变后调用
                document.getElementById ("info").innerHTML = "修改前值为: " + oldValue + ",修改后值为: " + newValue;
            })
          </script>
       </body>
    </html>
    

    运行结果:

    QQ20210817-104505-HD.gif

    二:样式绑定

    classstyleHTML元素的属性,用于设置元素的样式,我们可以用 v-bind来设置样式属性。
    v-bind 在处理 classstyle 时, 专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组。

    class属性绑定
    我们可以为 v-bind:class 设置一个对象,从而动态的切换 class:
    实例中将 isActive 设置为 true 显示了一个绿色的 div 块,如果设置为 false 则不显示:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <style>
    .active {
        width: 100px;
        height: 100px;
        background: green;
    }
    </style>
    </head>
    <body>
    <div id="app">
      <div v-bind:class="{ 'active': isActive }"></div>
    </div>
    
    <script>
    new Vue({
      el: '#app',
      data: {
        isActive: true
      }
    })
    </script>
    </body>
    </html>
    

    运行结果:

    image.png

    再看一个demo1:
    text-danger 类背景颜色覆盖了 active 类的背景色:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <style>
    .active {
        width: 100px;
        height: 100px;
        background: green;
    }
    .text-danger {
        background: red;
    }
    </style>
    </head>
    <body>
    <div id="app">
      <div class="static"
         v-bind:class="{ 'active': isActive, 'text-danger': hasError }">
      </div>
    </div>
    
    <script>
    new Vue({
      el: '#app',
      data: {
        isActive: true,
        hasError: true
      }
    })
    </script>
    </body>
    </html>
    

    运行结果:

    image.png

    再看一个demo2:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <style>
    .active {
        width: 100px;
        height: 100px;
        background: green;
    }
    .text-danger {
        background: red;
    }
    </style>
    </head>
    <body>
    <div id="app">
      <div v-bind:class="classObject"></div>
    </div>
    
    <script>
    new Vue({
      el: '#app',
      data: {
        classObject: {
          active: true,
          'text-danger': true
        }
      }
    })
    </script>
    </body>
    </html>
    

    运行结果:

    image.png

    再看一个demo3:
    我们还可以使用三元表达式来切换列表中的 class :

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <style>
    .active {
        width: 100px;
        height: 100px;
        background: green;
    }
    .text-danger {
        background: red;
    }
    </style>
    </head>
    <body>
    <div id="app">
      <div class="static"
         v-bind:class="{ 'active': isActive, 'text-danger': hasError }">
      </div>
    </div>
    
    <script>
    new Vue({
      el: '#app',
      data: {
        isActive: true,
        hasError: true
      }
    })
    </script>
    </body>
    </html>
    

    运行结果:

    image.png

    数组语法
    可以把一个数组传给 v-bind:class ,实例如下:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <style>
    .active {
        width: 100px;
        height: 100px;
        background: green;
    }
    .text-danger {
        background: red;
    }
    </style>
    </head>
    <body>
    <div id="app">
        <div v-bind:class="[activeClass, errorClass]"></div>
    </div>
    
    <script>
    new Vue({
      el: '#app',
      data: {
        activeClass: 'active',
        errorClass: 'text-danger'
      }
    })
    </script>
    </body>
    </html>
    

    运行结果:

    image.png

    demo2:
    errorClass 是始终存在的,isActive 为 true 时添加 activeClass 类:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <style>
    .text-danger {
        width: 100px;
        height: 100px;
        background: red;
    }
    .active {
        width: 100px;
        height: 100px;
        background: green;
    }
    </style>
    </head>
    <body>
    <div id="app">
        <div v-bind:class="[errorClass ,isActive ? activeClass : '']"></div>
    </div>
    
    <script>
    new Vue({
      el: '#app',
      data: {
        isActive: true,
        activeClass: 'active',
        errorClass: 'text-danger'
      }
    })
    </script>
    </body>
    </html>
    

    运行结果:

    image.png

    三:内联样式

    可以在 v-bind:style 直接设置样式:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    </head>
    <body>
    <div id="app">
        <div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">菜鸟教程</div>
    </div>
    
    <script>
    new Vue({
      el: '#app',
      data: {
        activeColor: 'green',
        fontSize: 30
      }
    })
    </script>
    </body>
    </html>
    

    结果:

    image.png

    demo2如下:
    直接绑定到一个样式对象,让模板更清晰

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    </head>
    <body>
    <div id="app">
      <div v-bind:style="styleObject">菜鸟教程</div>
    </div>
    
    <script>
    new Vue({
      el: '#app',
      data: {
        styleObject: {
          color: 'green',
          fontSize: '30px'
        }
      }
    })
    </script>
    </body>
    </html>
    

    结果:

    image.png

    demo3:
    v-bind:style可以使用数组将多个样式对象应用到一个元素上:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    </head>
    <body>
    <div id="app">
      <div v-bind:style="[baseStyles, overridingStyles]">菜鸟教程</div>
    </div>
    
    <script>
    new Vue({
      el: '#app',
      data: {
        baseStyles: {
          color: 'green',
          fontSize: '30px'
        },
        overridingStyles: {
          'font-weight': 'bold'
        }
      }
    })
    </script>
    </body>
    </html>
    

    结果:

    image.png

    四:事件处理器

    事件监听可以使用v-on指令:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    </head>
    <body>
    <div id="app">
      <button v-on:click="counter += 1">增加 1</button>
      <p>这个按钮被点击了 {{ counter }} 次。</p>
    </div>
    
    <script>
    new Vue({
      el: '#app',
      data: {
        counter: 0
      }
    })
    </script>
    </body>
    </html>
    

    结果点击一次就+1:

    //点击一次的结果
    这个按钮被点击了 1 次。
    

    通常情况下,我们需要使用一个方法来调用 JavaScript 方法。
    v-on 可以接收一个定义的方法来调用

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    </head>
    <body>
    <div id="app">
       <!-- `greet` 是在下面定义的方法名 -->
      <button v-on:click="greet">Greet</button>
    </div>
    
    <script>
    var app = new Vue({
      el: '#app',
      data: {
        name: 'Vue.js'
      },
      // 在 `methods` 对象中定义方法
      methods: {
        greet: function (event) {
          // `this` 在方法里指当前 Vue 实例
          alert('Hello ' + this.name + '!')
          // `event` 是原生 DOM 事件
          if (event) {
              alert(event.target.tagName)
          }
        }
      }
    })
    // 也可以用 JavaScript 直接调用方法
    app.greet() // -> 'Hello Vue.js!'
    </script>
    </body>
    </html>
    

    结果:

    image.png

    除了直接绑定到一个方法,也可以用内联 JavaScript 语句:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    </head>
    <body>
    <div id="app">
      <button v-on:click="say('hi')">Say hi</button>
      <button v-on:click="say('what')">Say what</button>
    </div>
    
    <script>
    new Vue({
      el: '#app',
      methods: {
        say: function (message) {
          alert(message)
        }
      }
    })
    </script>
    </body>
    </html>
    
    image.png
    image.png

    四:事件修饰符

    Vue.jsv-on提供了事件修饰符来处理 DOM 事件细节,如:event.preventDefault()event.stopPropagation()
    Vue.js通过由点 . 表示的指令后缀来调用修饰符。

    • .stop - 阻止冒泡
    • .prevent - 阻止默认事件
    • .capture - 阻止捕获
    • .self - 只监听触发该元素的事件
    • .once - 只触发一次
    • .left - 左键事件
    • .right - 右键事件
    • .middle - 中间滚轮事件
    <!-- 阻止单击事件冒泡 -->
    <a v-on:click.stop="doThis"></a>
    <!-- 提交事件不再重载页面 -->
    <form v-on:submit.prevent="onSubmit"></form>
    <!-- 修饰符可以串联  -->
    <a v-on:click.stop.prevent="doThat"></a>
    <!-- 只有修饰符 -->
    <form v-on:submit.prevent></form>
    <!-- 添加事件侦听器时使用事件捕获模式 -->
    <div v-on:click.capture="doThis">...</div>
    <!-- 只当事件在该元素本身(而不是子元素)触发时触发回调 -->
    <div v-on:click.self="doThat">...</div>
    <!-- click 事件只能点击一次,2.1.4版本新增 -->
    <a v-on:click.once="doThis"></a>
    
    

    按键修饰符
    Vue 允许为v-on 在监听键盘事件时添加按键修饰符:

    <!-- 只有在 keyCode 是 13 时调用 vm.submit() -->
    <input v-on:keyup.13="submit">
    

    记住所有的keyCode比较困难,所以 Vue 为最常用的按键提供了别名:

    <!-- 同上 -->
    <input v-on:keyup.enter="submit">
    <!-- 缩写语法 -->
    <input @keyup.enter="submit">
    

    全部的按键别名:

    • .enter
    • .tab
    • .delete (捕获 "删除" 和 "退格" 键)
    • .esc
    • .space
    • .up
    • .down
    • .left
    • .right
    • .ctrl
    • .alt
    • .shift
    • .meta

    实例

    <p><!-- Alt + C -->
    <input @keyup.alt.67="clear">
    <!-- Ctrl + Click -->
    <div @click.ctrl="doSomething">Do something</div>
    

    相关文章

      网友评论

          本文标题:Vue-(3)循环-计算-监听-样式绑定-事件处理

          本文链接:https://www.haomeiwen.com/subject/xgbbbltx.html