![](https://img2020.cnblogs.com/blog/2216408/202104/2216408-20210402094128253-538287885.png)
<!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>计数器</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style>
button {
float: left;
width: 50px;
height: 25px;
}
span {
float: left;
width: 25px;
height: 25px;
font-size: 20px;
text-align: center;
}
</style>
</head>
<body>
<div id="app">
<button value="+" @click="add">+</button>
<span>{{num}}</span>
<button value="-" @click="sub">-</button>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
num: 1
},
methods: {
add: function() {
this.num++;
},
sub: function() {
this.num--;
}
}
})
</script>
</body>
</html>