method没加s加上就好了
<body>
<div id="app">
<button @click="sub">-</button>
<span>{{code}}</span>
<button @click="add">+</button>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
code: 0
},
methods: {
add: function () {
if (this.code < 10) {
this.code++;
} else {
alert('到头啦!不要再加啦');
}
},
sub: function () {
if (this.code > 0) {
this.code--;
} else {
alert('到头啦!不要再减啦');
}
}
}
})
</script>
</body>