欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue.js實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能

 更新時(shí)間:2020年02月22日 14:55:36   作者:JTz666666  
這篇文章主要為大家詳細(xì)介紹了vue.js實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

使用vue.js來(lái)編寫(xiě)一個(gè)簡(jiǎn)單的計(jì)算器,供大家參考,具體內(nèi)容如下

效果如圖所示:是一個(gè)十分簡(jiǎn)單的計(jì)算器,包含了加減乘除,不是用原生js寫(xiě)的,而是用vue.js寫(xiě)的

html:

<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8">
 <title></title>
 </head>
 <body>
 <div id="app">
 
 <input type="text" v-model="n1" />
 <select v-model="opt">
 <option value="+">+</option>
 <option value="-">-</option>
 <option value="*">*</option>
 <option value="/">/</option>
 </select>
 
 <input type="text" v-model="n2" />
 <input type="button" value="=" @click="calc" />
 <input type="text" v-model="result" />
 </div>

 </body>
</html>

js代碼:

<script src="js/vue.js"></script>
 <script>
 var vm=new Vue({
 el:"#app",
 data:{
  n1:0,
  n2:0,
  result:0,
  opt:"+"
 },
 methods:{
  //定義計(jì)算器算數(shù)的方法
  calc(){
  switch(this.opt){
  case "+":
  this.result=parseInt(this.n1)+parseInt(this.n2)
  //return this.result
  break;
  case "-":
  this.result=parseInt(this.n1)-parseInt(this.n2)
  //return this.result
  break;
  case "*":
  this.result=parseInt(this.n1)*parseInt(this.n2)
  //return this.result
  break;
  case "/":
  this.result=parseInt(this.n1)/parseInt(this.n2)
  //return this.result
  break;
  }
  
  }
 }
 })
</script>

不過(guò)在最后我使用了一個(gè)swith循環(huán)來(lái)設(shè)置這個(gè),還有另一種方法,代碼量更少:
可以把里面的循環(huán)改成:

//這是投機(jī)取巧,不要經(jīng)常用 正是開(kāi)發(fā)中,盡量少用
var codeStr='parseInt(this.n1)'+this.opt+'parseInt(this.n2)'
this.result=eval(codeStr)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論