用vue写一个计算器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="./vue.js"></script>
</head>
<body>
<div id="app">
<!-- 第一个值 -->
<input type="text" v-model.number="a" />
<select v-model="opt">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
<!-- 第二个值 -->
<input type="text" v-model.number="b" />
<input type="button" value="=" @click="calc" />
<!-- 答案 -->
<input type="text" v-model="result" />
</div>
<script>
let vm = new Vue({
el: "#app",
data: {
a: 0,
b: 0,
result: 0,
opt: "+",
},
methods: {
calc: function () {
if (this.opt == "+") {
return (this.result = this.a + this.b);
} else if (this.opt == "-") {
return (this.result = this.a - this.b);
} else if (this.opt == "*") {
return (this.result = this.a * this.b);
} else if (this.opt == "/") {
return (this.result = this.a / this.b);
}
},
},
});
</script>
</body>
</html>
效果如下: 图片
