1、Vue如何处理事件
- v-on指令用法
<input type="button" v-on:click='num++'/>
- v-once简写形式
<input type="button" @click='num++'/>
2、事件函数的调用方式
- 直接绑定函数名称
<button @click='handler'>点击2</button>
- 调用函数
例:<button @click='handler()'>点击3</button>
-
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Vue基本使用</title> <style> [v-cloak] { display: none; } </style> <!-- 引入vue.js --> <script src="js/vuejs-2.5.16.js"></script> </head> <body> <div id="app"> <div>{{num}}</div> <div> <button v-on:click='num++'>点击</button> <button @click='num++'>点击1</button> <button @click='handler'>点击2</button> <button @click='handler()'>点击3</button> </div> </div> <script> var vue = new Vue({ // 元素的挂载位置(值可以是Css选择器或DOM元素) el: '#app', // 模型数据(值是一个对象) data: { num: 0 }, methods: { handler: function () { // 这里的this是vue的实例对象 console.log(this === vue); this.num++; } } }); </script> </body> </html>