小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Vue實(shí)現(xiàn)簡(jiǎn)易計(jì)算器

 流楚丶格念 2022-01-14

文章目錄

    •  

在這里插入圖片描述

1. HTML 代碼結(jié)構(gòu)

  <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>

2. Vue實(shí)例代碼:

  // 創(chuàng)建 Vue 實(shí)例,得到 ViewModelvar vm = new Vue({  el: '#app',  data: {n1: 0,n2: 0,result: 0,opt: '+'  },  methods: {calc() { // 計(jì)算器算數(shù)的方法    // 邏輯:   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;  } 

          // 注意:這是投機(jī)取巧的方式,正式開(kāi)發(fā)中,盡量少用  // var codeStr = 'parseInt(this.n1) ' + this.opt + ' parseInt(this.n2)'  // this.result = eval(codeStr)}  }});

3. 全部代碼

<!DOCTYPE html><html lang="en"><head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="./lib/vue-2.4.0.js"></script></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>

  <script>// 創(chuàng)建 Vue 實(shí)例,得到 ViewModelvar vm = new Vue({  el: '#app',  data: {n1: 0,n2: 0,result: 0,opt: '+'  },  methods: {calc() { // 計(jì)算器算數(shù)的方法    // 邏輯:   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)  breakvalue_name: value,            case '/':  this.result = parseInt(this.n1) / parseInt(this.n2)  break;  } 

          // 注意:這是投機(jī)取巧的方式,正式開(kāi)發(fā)中,盡量少用  // var codeStr = 'parseInt(this.n1) ' + this.opt + ' parseInt(this.n2)'  // this.result = eval(codeStr)}  }});
  </script></body></html>

    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多