• v-on为按钮绑定事件


            <!--事件中直接写js片段-->
            <button v-on:click="num++">点赞</button>
            <!--事件指定一个回调函数,必须是Vue实例中定义的函数-->
            <button @click="cancle">取消</button>
    

      

    上面是为两个按钮绑定了单击事件,其中一个对于num进行自增,另外一个自减。

    v-on:click也可以写作@click

    事件的冒泡:

            <!-- 事件修饰符 -->
            <div style="border: 1px solid red;padding: 20px;" v-on:click="hello">
                大div
                <div style="border: 1px solid blue;padding: 20px;" @click="hello">
                    小div <br />
                    <a href="http://www.baidu.com" @click.prevent="hello">去百度</a>
                </div>
            </div>
    

      

    上面的这两个嵌套div中,如果点击了内层的div,则外层的div也会被触发;这种问题可以事件修饰符来完成:

            <!-- 事件修饰符 -->
            <div style="border: 1px solid red;padding: 20px;" v-on:click.once="hello">
                大div
                <div style="border: 1px solid blue;padding: 20px;" @click.stop="hello">
                    小div <br />
                    <a href="http://www.baidu.com" @click.prevent.stop="hello">去百度</a>
                    <!--这里禁止了超链接的点击跳转操作,并且只会触发当前对象的操作-->
                </div>
            </div>
    

      

    关于事件修饰符:

     

    按键修饰符:

     

    v-for遍历循环

    <!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">
            <ul>
                <li v-for="(user,index) in users" :key="user.name" v-if="user.gender == '女'">
                    <!-- 1、显示user信息:v-for="item in items" -->
                   当前索引:{{index}} ==> {{user.name}}  ==>   {{user.gender}} ==>{{user.age}} <br>
                    <!-- 2、获取数组下标:v-for="(item,index) in items" -->
                    <!-- 3、遍历对象:
                            v-for="value in object"
                            v-for="(value,key) in object"
                            v-for="(value,key,index) in object" 
                    -->
                    对象信息:
                    <span v-for="(v,k,i) in user">{{k}}=={{v}}=={{i}};</span>
                    <!-- 4、遍历的时候都加上:key来区分不同数据,提高vue渲染效率 -->
                </li>
    
                
            </ul>
    
            <ul>
                <li v-for="(num,index) in nums" :key="index"></li>
            </ul>
        </div>
        <script src="../node_modules/vue/dist/vue.js"></script>
        <script>         
            let app = new Vue({
                el: "#app",
                data: {
                    users: [{ name: '柳岩', gender: '女', age: 21 },
                    { name: '张三', gender: '男', age: 18 },
                    { name: '范冰冰', gender: '女', age: 24 },
                    { name: '刘亦菲', gender: '女', age: 18 },
                    { name: '古力娜扎', gender: '女', age: 25 }],
                    nums: [1,2,3,4,4]
                },
            })
        </script>
    </body>
    
    </html>
    

      

  • 相关阅读:
    LeetCode 1447. Simplified Fractions
    LeetCode 717. 1bit and 2bit Characters
    LeetCode 1725. Number Of Rectangles That Can Form The Largest Square
    LeetCode 2016. Maximum Difference Between Increasing Elements
    LeetCode 二叉树遍历算法题解 All In One
    Top Universities in China Open Source Course Materials All In One
    SQL 查询语句: 字符串正则匹配 All In One
    React memo & useMemo All In One All In One
    webpack 性能优化 All In One
    漫画教程: HTTPS 的工作原理 (中文版)All In One
  • 原文地址:https://www.cnblogs.com/vincentmax/p/14374085.html
Copyright © 2020-2023  润新知