vue事件绑定@click

事件绑定
1.Vue如果处理事件?

  • v-on指令用法
<input type= 'button' v-on:click = 'num++'/>
  • v-on简写形式
<input type = 'button' @click = 'num++' />

2.事件调用函数方式

  • 直接绑定函数名称
<button v-on:click='say'>Hello</button>
  • 调用函数
<button v-on:click='say()'>Say hi</button>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src = "js/vue.js"></script>
</head>
<body>
    <div id = "app">
        <div>{{num}}</div>
        <button v-on:click = "num++">点击1</button>
        <button @click = "num++">点击2</button>
        <button @click = "handle">点击3</button>
        <button @click = "handle()">点击4</button>
    </div>
    
    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                num: '0'
            },
            methods:{
                handle:function(){
                    this.num++;
                }
            }
        })
    </script>
</body>
</html>
Logo

前往低代码交流专区

更多推荐