<!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>Document</title>
</head>
<body>
<div id="app">
得分:{{ count }}
<button @click="add">加分</button>
<button @click="minus">减分</button>
<br>
<input type="text" @keyup.enter="onEnter" v-model="secondCount" placeholder="输入数值按回车加到得分上">
</div>
<script src="node_modules/vue/dist/vue.js"></script>
<script>
new Vue({
el: '#app',
data() {
return {
count: 0,
secondCount: ''
}
},
methods: {
add() {
this.count++;
},
minus() {
this.count--;
},
onEnter() {
// 文本框的数字会默认转成字符串,此处用parseInt()进行整数转换
this.count = this.count + parseInt(this.secondCount);
}
}
});
</script>
</body>
</html>