日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Vue實現(xiàn)簡易計算器

 流楚丶格念 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實例代碼:

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

          // 注意:這是投機取巧的方式,正式開發(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 實例,得到 ViewModelvar vm = new Vue({  el: '#app',  data: {n1: 0,n2: 0,result: 0,opt: '+'  },  methods: {calc() { // 計算器算數(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;  } 

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

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

    0條評論

    發(fā)表

    請遵守用戶 評論公約