1. 程式人生 > 其它 >06-v-on的基本使用

06-v-on的基本使用

技術標籤:vuevue

v-on可以繫結很多時間監聽,如:click(點選時間),keyup(鍵盤彈起事件)等

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head> <body> <script src="../js/vue.js"></script> <div id="app"> <h2>{{counter}}</h2> <!-- 第一種寫法:直接在指令中使用 --> <!-- <button v-on:click="counter++">+</button> <button v-on:click="counter--">-</button> -->
<!-- 第二種寫法:使用methods屬性 --> <!-- <button v-on:click="increment">+</button> <button v-on:click="subcrement">-</button> --> <!-- 第三種寫法:語法糖 --> <button @click="increment">+</button> <button @click
="subcrement">
-</button> </div> <script> const app=new Vue({ el: '#app', data: { counter:0 }, methods:{ increment(){ this.counter++; }, subcrement(){ this.counter--; } } }) </script> </body> </html>