vue-model 간이 계산기 구현

2976 단어 vuemodel계산기
본고의 실례는 여러분에게 vue-model이 간이 계산기를 실현하는 구체적인 코드를 공유하여 참고하도록 하였으며, 구체적인 내용은 다음과 같다.

<!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>Vue</title>
 <script src="../lib/vue-2.4.0.js"></script>
</head>
<body>
 
 <div id="app" >
  <!--   -->
  <input type="text" v-model='n1' placeholder="0">
  <!--   -->
  <select v-model='opt'>
   <option value="+"> + </option>
   <option value="-">-</option>
   <option value="*">*</option>
   <option value="/">/</option>
  </select>
  <!--  2 -->
  <input type="text" v-model='n2' placeholder="0">
  <!--   -->
  <input type="button" value='=' >
  <!--   -->
  <input type="text" v-model='result' placeholder="0">
  <!--   -->
  <input type="button" value=' ' @click='calc'>
  <!--   -->
  <input type="button" value=' ' @click='zero'>


 </div>

 <script>
  var vm = new Vue({
   el: '#app', // new 
   data: { //data  el  
    n1: '',
    n2:'',
    result:'',
    opt: '+'
   },
   methods:{
    calc(){
     // 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;
     // }

     //  
     var codeStr = 'parseInt(this.n1) '+ this.opt +' parseInt(this.n2)'
     this.result = eval(codeStr)
    },
    zero(){
     this.n1 = '',
     this.n2 = '',
     this.result = '',
     this.opt = '+'
    }

   }
  }) 
 </script>

</body>
</html>
계산기 관련 기술 기사는 테마를 클릭하십시오javascript 계산기 기능 요약
이상은 본문의 전체 내용입니다. 여러분의 학습에 도움이 되고 저희를 많이 응원해 주십시오.

좋은 웹페이지 즐겨찾기