new Vue({
el: '#app',
data: {
num: 0,
colour: ""
},
methods: {
subFn() {
if (this.num == 0) return
this.num = this.num - 1
},
addFn() {
this.num = this.num + 1
},
redFn() {
this.colour = "red"
}
}
})
<div id="app">
<div>
<button @click="subFn()">减少</button>
<p :class="colour">当前计数:{{this.num}}</p>
<button @click="addFn()">增加</button>
<button @click="redFn()">计数变红色</button>
</div>
</div>
.red{
color: red;
}