<!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>
</head>
<body>
<div id="app">
<button @click="sub">-</button>
<span v-text="num"></span>
<button @click="add">+</button>
<button @click="reset">重置</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var app = new Vue({
el: '#app',
data: {
num: 5
},
methods: {
"sub": function () {
console.log('sub')
if (this.judgmentNum(this.num)) {
this.num--;
}
},
"add": function () {
console.log('add')
if (this.judgmentNum(this.num)) {
this.num++;
}
},
"reset": function () {
console.log('reset')
this.num = 5
},
"judgmentNum": function (num) {
console.log('num ==' + num)
if (num <= 0) {
return alert("别点了最小值了")
}
if (num >= 10) {
return alert("别点了最大值了")
}
return true
}
}
})
</script>
</body>
</html>