v-on
-
注册事件 = 添加监听 +提供逻辑处理
-
语法
-
v-on 事件名 = 内联语句
-
v-on 事件名 = methods中的函数名
-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 200px;
height: 100px;
background-color: pink;
border: solid 4px skyblue;
border-radius: 5px;
box-shadow: 5px;
}
</style>
</head>
<body>
<div id="app">
<div class="box">
<h3>自动售货机</h3>
<button @click=buy1()>可乐 5元</button>
<button @click=buy2()>咖啡 10元</button>
</div>
<p>账户余额: {{money}} 元</p>
</div>
<script src="/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
// 数据
data: {
money: 100
},
// 方法
methods: {
buy1() {
this.money -= 5
},
buy2() {
this.money -= 10
}
}
})
</script>
</body>
</html>