事件指令(方法事件处理器)

代码

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="vue.js"></script>
</head>
<body>
<div id="app">
    <h2>v-on:事件名='函数' : 给这个标签绑定一个事件,当触发这个事件,就会执行函数</h2>
    <!--    -事件:click,blur,dbclick。。。很多-->
    <!--    -函数 必须写在methods 对象内,可以使用es6的对象写法-->
    <!--    # 重点:-->
    <!--    v-on:事件名='函数'    可以简写成      @事件名='函数'(以后都用它)-->
    <button v-on:click="clickEvent" v-show="b1">click</button>
    <button @click="clickEvent1" v-show="b2">click1</button>
</div>
</body>
<script>
    <!--    创建app实例-->
    const app = Vue.createApp({
        data() {
            return {
                input1: '<input type="text">',
                text: '新的',
                b1: true,
                b2: true,
            }
        }, methods: {
            // es6
            clickEvent() {
                this.b2 = !this.b2
            },
            // es5
            clickEvent1: function () {
                this.b1 = !this.b1
            }
        }
    })
    //挂载
    app.mount('#app')

</script>
</html>
posted @ 2022-10-18 17:33  Sherwin_szw  阅读(26)  评论(0编辑  收藏  举报