“Vue 事件处理”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
(建立内容为“https://www.bilibili.com/video/BV1Zy4y1K7SH/?p=14”的新页面)
 
 
(未显示同一用户的1个中间版本)
第1行: 第1行:
 
https://www.bilibili.com/video/BV1Zy4y1K7SH/?p=14
 
https://www.bilibili.com/video/BV1Zy4y1K7SH/?p=14
 +
 +
事件的基本使用:
 +
 +
            1.使用v-on:xxx 或 @xxx 绑定事件,其中 xxx 是事件名;
 +
 +
            2.事件的回调需要配置在 methods 对象中,最终会在 vm 上;
 +
 +
            3.methods 中配置的函数,不要用箭头函数!否则 this 就不是 vm 了;
 +
 +
            4.methods 中配置的函数,都是被 Vue 所管理的函数,this 的指向是 vm 或组件实例对象;
 +
 +
            5.@click="demo" 和 @click="demo($event)" 效果一致,但后者可以传参;<syntaxhighlight lang="html">
 +
<!DOCTYPE html>
 +
<html lang="en">
 +
<head>
 +
    <meta charset="UTF-8">
 +
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
 +
    <title>事件的基本使用</title>
 +
    <!-- 引入 Vue -->
 +
    <script type="text/javascript" src="../js/vue.js"></script>
 +
</head>
 +
<body>
 +
    <!-- 事件的基本使用:
 +
            1.使用v-on:xxx 或 @xxx 绑定事件,其中 xxx 是事件名;
 +
            2.事件的回调需要配置在 methods 对象中,最终会在 vm 上;
 +
            3.methods 中配置的函数,不要用箭头函数!否则 this 就不是 vm 了;
 +
            4.methods 中配置的函数,都是被 Vue 所管理的函数,this 的指向是 vm 或组件实例对象;
 +
            5.@click="demo" 和 @click="demo($event)" 效果一致,但后者可以传参;
 +
      -->
 +
    <!-- 准备好一个容器 -->
 +
    <div id="root">
 +
        <h2>欢迎来到{{name}}</h2>
 +
        <button v-on:click="showInfo1">点我提示信息1(不传参)</button>
 +
        <button @click="showInfo2($event, 66)">点我提示信息2(传参)</button>
 +
    </div>
 +
</body>
 +
<script type="text/javascript">
 +
    const vm = new Vue({
 +
        el:'#root',
 +
        data:{
 +
            name:'北京'
 +
        },
 +
        methods:{
 +
            showInfo1(event){
 +
                // console.log(event.target.innerText);
 +
                // console.log(this === vm); //此处的 this 是 vm
 +
                alert('同学你好_1');
 +
            },
 +
            showInfo2(event, number){
 +
                console.log(event, number);
 +
                // console.log(event.target.innerText);
 +
                // console.log(this === vm); //此处的 this 是 vm
 +
                alert('同学你好_2');
 +
            }
 +
        }
 +
    })
 +
</script>
 +
</html>
 +
</syntaxhighlight>

2024年7月28日 (日) 13:48的最新版本

https://www.bilibili.com/video/BV1Zy4y1K7SH/?p=14

事件的基本使用:

            1.使用v-on:xxx 或 @xxx 绑定事件,其中 xxx 是事件名;

            2.事件的回调需要配置在 methods 对象中,最终会在 vm 上;

            3.methods 中配置的函数,不要用箭头函数!否则 this 就不是 vm 了;

            4.methods 中配置的函数,都是被 Vue 所管理的函数,this 的指向是 vm 或组件实例对象;

            5.@click="demo" 和 @click="demo($event)" 效果一致,但后者可以传参;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>事件的基本使用</title>
    <!-- 引入 Vue -->
    <script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
    <!-- 事件的基本使用:
            1.使用v-on:xxx 或 @xxx 绑定事件,其中 xxx 是事件名;
            2.事件的回调需要配置在 methods 对象中,最终会在 vm 上;
            3.methods 中配置的函数,不要用箭头函数!否则 this 就不是 vm 了;
            4.methods 中配置的函数,都是被 Vue 所管理的函数,this 的指向是 vm 或组件实例对象;
            5.@click="demo" 和 @click="demo($event)" 效果一致,但后者可以传参;
      -->
    <!-- 准备好一个容器 -->
    <div id="root">
        <h2>欢迎来到{{name}}</h2>
        <button v-on:click="showInfo1">点我提示信息1(不传参)</button>
        <button @click="showInfo2($event, 66)">点我提示信息2(传参)</button>
    </div>
</body>
<script type="text/javascript">
    const vm = new Vue({
        el:'#root',
        data:{
            name:'北京'
        },
        methods:{
            showInfo1(event){
                // console.log(event.target.innerText);
                // console.log(this === vm); //此处的 this 是 vm
                alert('同学你好_1');
            },
            showInfo2(event, number){
                console.log(event, number);
                // console.log(event.target.innerText);
                // console.log(this === vm); //此处的 this 是 vm
                alert('同学你好_2');
            }
        }
    })
</script>
</html>