使用Vue三種方法實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
代碼實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的計(jì)算器,用戶可以在輸入框中輸入兩個(gè)數(shù)字,選擇一個(gè)操作符,并點(diǎn)擊“等于”按鈕,Vue.js會(huì)根據(jù)用戶的輸入進(jìn)行計(jì)算,并將結(jié)果顯示在另一個(gè)輸入框中。
以下是效果圖
第一種:使用methods方法實(shí)現(xiàn)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="js/vue.js"></script>
</head>
<body>
<div id="app">
<input type="number" placeholder="請(qǐng)輸入數(shù)字" v-model.number="number1">
<select v-model="pwd">
<option>請(qǐng)選擇符號(hào)</option>
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="number" placeholder="請(qǐng)輸入數(shù)字" v-model.number="number2" >
<button @click="pd()">等于</button>
<input type="text" v-model="result" placeholder="結(jié)果">
</div>
</body>
<script>
var vm = new Vue({
el:"#app",
data:{
number1:'',
number2:'',
pwd:'請(qǐng)選擇符號(hào)',
result:''
},
methods:{
pd(){
switch (this.pwd){
case '+':
this.result = this.number1+this.number2;
break;
case '-':
this.result = this.number1-this.number2;
break;
case '*':
this.result = this.number1*this.number2;
break;
case '/':
this.result = this.number1/this.number2;
break;
}
}
}
})
</script>
</html>
1:首先,引入Vue.js庫(kù)。在代碼中使用了script 標(biāo)簽引入Vue.js文件
2:methods屬性中定義了一個(gè)方法pd(),用于根據(jù)用戶的輸入計(jì)算結(jié)果,并將結(jié)果保存到result變量中。
3:使用v-model.number指令將輸入框中的值綁定到number1和number2變量上,從而實(shí)現(xiàn)實(shí)時(shí)更新數(shù)據(jù)。使用.number修飾符將輸入值轉(zhuǎn)換為數(shù)字類型。
4:Vue.js使用@click指令綁定一個(gè)點(diǎn)擊事件,并調(diào)用pd()方法,根據(jù)用戶輸入的數(shù)字和操作符計(jì)算結(jié)果,并將結(jié)果保存到result變量中。使用v-model指令將result變量的值綁定到結(jié)果顯示輸入框中,從而實(shí)現(xiàn)實(shí)時(shí)更新結(jié)果。
第二種:使用watch方法實(shí)現(xiàn)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="js/vue.js"></script>
</head>
<body>
<div id="app">
<input type="number" placeholder="請(qǐng)輸入數(shù)字" v-model.number="number1">
<select v-model="pwd">
<option>請(qǐng)選擇符號(hào)</option>
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="number" placeholder="請(qǐng)輸入數(shù)字" v-model.number="number2" >
<button @click="pd()">等于</button>
<input type="text" v-model="result" placeholder="結(jié)果">
</div>
</body>
<script>
var vm=new Vue({
el:"#app",
data:{
number1:0,
number2:0,
pwd:"+",
result:0
},
computed:{
add(){
var num1 = this.number1
var num2 = this.number2
var num3 = this.pwd
return{
num1,
num2,
num3
}
}
},
watch:{
add(val){
switch(this.pwd){
case '+':
this.result = this.number1+this.number2;
break;
case '-':
this.result = this.number1-this.number2;
break;
case '*':
this.result = this.number1*this.number2;
break;
case '/':
this.result = this.number1/this.number2;
break;
}
}
}
});
</script>
</html>
在Vue實(shí)例中,通過(guò)watch屬性監(jiān)聽(tīng)add計(jì)算屬性的變化,當(dāng)add屬性發(fā)生變化時(shí),執(zhí)行一個(gè)函數(shù)來(lái)計(jì)算結(jié)果。函數(shù)中通過(guò)switch語(yǔ)句根據(jù)pwd的值來(lái)選擇不同的運(yùn)算方式,最終將計(jì)算結(jié)果賦值給result屬性。
第三種:使用computed實(shí)現(xiàn)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-482406.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="js/vue.js"></script>
</head>
<body>
<div id="app">
<input type="number" placeholder="請(qǐng)輸入數(shù)字" v-model.number="number1">
<select v-model="pwd">
<option>請(qǐng)選擇符號(hào)</option>
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="number" placeholder="請(qǐng)輸入數(shù)字" v-model.number="number2" >
<button @click="pd()">等于</button>
<input type="text" v-model="result" placeholder="結(jié)果">
</div>
</body>
<script>
var vm =new Vue({
el:"#app",
data:{
number1:0,
number2:0,
pwd:"+",
result:0
},
computed:{
pd(){
switch(this.opt){
case "+":
this.result =parseInt(this.n1) + parseInt(this.n2)
break;
case "-":
this.result =parseInt(this.n1) - parseInt(this.n2)
break;
case "*":
this.result =parseInt(this.n1) * parseInt(this.n2)
break;
case "/":
this.result =parseInt(this.n1) / parseInt(this.n2)
break;
}
}
},
})
</script>
</html>
通過(guò)computed屬性定義了一個(gè)計(jì)算屬性pd,該計(jì)算屬性執(zhí)行一個(gè)函數(shù)來(lái)計(jì)算結(jié)果。函數(shù)中通過(guò)switch語(yǔ)句根據(jù)pwd的值來(lái)選擇不同的運(yùn)算方式,最終將計(jì)算結(jié)果賦值給result屬性。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-482406.html
到了這里,關(guān)于使用Vue三種方法實(shí)現(xiàn)簡(jiǎn)單計(jì)算器的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!