Vue---基于v-model的简易计算器

<input type="text" v-model="n1">

 <select name="" id="" 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">
data () {
    return {
      msg: '猥琐发育,别浪~',
      n1: 0,
      n2: 0,
      result: 0,
      opt: '+'
    }
  }
calc: function(){

          switch(this.calc){
              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;
          }

        // var codeStr = 'parseInt(this.n1)'+ this.opt +' parseInt(this.n2)';
        // this.result = eval(codeStr);

      }
发布了33 篇原创文章 · 获赞 7 · 访问量 3235

猜你喜欢

转载自blog.csdn.net/GaoXiR/article/details/103736281
今日推荐