위 챗 애플 릿 이 계산기 기능 을 실현 하 다.

본 논문 의 사례 는 여러분 에 게 위 챗 애플 릿 이 계산기 기능 을 실현 하 는 구체 적 인 코드 를 공유 하 였 으 며,구체 적 인 내용 은 다음 과 같 습 니 다.
1.위 챗 애플 릿 개발 도구 인터페이스

2.디 렉 터 리 구조
페이지 에 처음 들 어 온 디 렉 터 리 구 조 는 다음 과 같 습 니 다.

3.주의해 야 할 문제
(1)추 가 된 새 페이지 파일 은 app.json 에서 설정 해 야 합 니 다.그렇지 않 으 면 페이지 가 잘못 되 었 습 니 다.

(2)작업 원리  에 이벤트 bindtap="btnClick"id="{{n9}}"를 추가 합 니 다.   클릭 이벤트 에 해당 합 니 다.
js 코드 에서 this.data.n9 를 통 해 데 이 터 를 얻 을 수 있 습 니 다.이 데이터 의 정 의 는 모두 js 에 있 습 니 다.

는 id 를 작성 하고 구체 적 인 함수 에서 이벤트.target.id 는 id 가 얼마 인지 판단 하여 구분 합 니 다.서로 다른 탭 의 클릭 을 통 해 업무 논 리 를 할 수 있 습 니 다.데이터 에 접근 할 필요 가 있다 면 this.data.xx 를 통 해.
계산기 wxml 페이지

<view class="content">
  <view class="xianshi">{{screenNum}}</view>
  <view class="anniu">
    <view class="item blue" bindtap="btnClick" id="{{n9}}">9</view>
    <view class="item blue" bindtap="btnClick" id="{{n8}}">8</view>
    <view class="item blue" bindtap="btnClick" id="{{n7}}">7</view>
    <view class="item blue" bindtap="btnClick" id="{{na}}">+</view>
  </view>
   <view class="anniu">
    <view class="item blue" bindtap="btnClick" id="{{n6}}">6</view>
    <view class="item blue" bindtap="btnClick" id="{{n5}}">5</view>
    <view class="item blue" bindtap="btnClick" id="{{n4}}">4</view>
    <view class="item blue" bindtap="btnClick" id="{{nb}}">-</view>
  </view>
   <view class="anniu">
    <view class="item blue" bindtap="btnClick" id="{{n3}}">3</view>
    <view class="item blue" bindtap="btnClick" id="{{n2}}">2</view>
    <view class="item blue" bindtap="btnClick" id="{{n1}}">1</view>
    <view class="item blue" bindtap="btnClick" id="{{nc}}">*</view>
  </view>
   <view class="anniu">
    <view class="item blue" bindtap="btnClick" id="{{n0}}">0</view>
    <view class="item blue" bindtap="btnClear">AC</view>
    <view class="item blue" bindtap="btnJs">=</view>
    <view class="item blue" bindtap="btnClick" id="{{nd}}">/</view>
  </view>
</view>

// pages/cal/cal.js
Page({
 
  /**
   *        
   */
  data: {
   n0: 0,
   n1: 1,
   n2: 2,
   n3: 3,
   n4: 4,
   n5: 5,
   n6: 6,
   n7: 7,
   n8: 8,
   n9: 9,
   na: '+',
   nb: '-',
   nc: '*',
   nd: '/',
   screenNum: 0,
   screenStr: 0,
   is_num:1
  },
 
  /**
   *       --      
   */
  onLoad: function (options) {
  
  },
 
  /**
   *       --          
   */
  onReady: function () {
  
  },
 
  /**
   *       --      
   */
  onShow: function () {
  
  },
 
  /**
   *       --      
   */
  onHide: function () {
  
  },
 
  /**
   *       --      
   */
  onUnload: function () {
  
  },
 
  /**
   *           --        
   */
  onPullDownRefresh: function () {
  
  },
 
  /**
   *              
   */
  onReachBottom: function () {
  
  },
 
  /**
   *          
   */
  onShareAppMessage: function () {
  
  },
  btnClick:function(event){
    //console.log('     '+event.target.id);
    //console.log('   ' + this.data.is_num);
    var op='';
    var data=0;
    var last_is_num = this.data.is_num;
    //        
    if (event.target.id == '9' || event.target.id == '8' || event.target.id == '7' || event.target.id == '6' || event.target.id == '5' || event.target.id == '4' || event.target.id == '3' || event.target.id == '2' || event.target.id == '1' || event.target.id == '0') {
      data = event.target.id;
      this.setData({ is_num: 1 });
    }
    if (event.target.id == '+' || event.target.id == '-' || event.target.id == '*' || event.target.id == '/') {
      op = event.target.id;
      this.setData({ is_num: 0 });
    }
    if (last_is_num==1){
      //        
      if (op == ''){
        //      
        if (this.data.screenNum!=0){
          this.setData({ screenNum: this.data.screenNum + data });
          this.setData({ screenStr: this.data.screenStr + data });
        }else{
          this.setData({ screenNum: data});
          this.setData({ screenStr: data });
        }
      }else{
        this.setData({ screenNum: this.data.screenNum + op });
        this.setData({ screenStr: this.data.screenStr +',' +op+',' });
      }
    }else{
      //      
      if (data != 0) {
        //      
        this.setData({ screenNum: this.data.screenNum + data });
        this.setData({ screenStr: this.data.screenStr + data });
      } else {
        return;
      }
    }
    //console.log(op+'aaaaa'+data);
    //console.log('   '+this.data.is_num);
    //console.log('screenNum' + this.data.screenNum);
    //console.log(this.data.screenStr);
  },
  btnJs:function(){
    console.log(this.data.screenNum);
    console.log(this.data.screenStr);
    var result=0;
    var strs = new Array(); //      
    strs = this.data.screenStr.split(","); //    
    for (var i = 0; i < strs.length; i++) {
      //console.log(strs[i] + i); //        
      if (strs[i]=='+'){
        result = parseInt(strs[i - 1]) + parseInt(strs[i+1]);
      }
      if (strs[i] == '-') {
        result = strs[i - 1] - strs[i + 1];
      }
      if (strs[i] == '*') {
        result = strs[i - 1] * strs[i + 1];
      }
      if (strs[i] == '/') {
        result = strs[i - 1] / strs[i + 1];
      }    
    }
    console.log('result:'+result);
    this.setData({ screenNum: result});
    this.setData({ screenStr: result });    
  },
  btnClear:function(){
    //          
    this.setData({ screenNum: 0 });
    this.setData({ screenStr: 0 });
    this.setData({ is_num: 1 });      
  }
})
요약 하면 애플 릿 의 구조 에 있어 상대 적 인 단위 rpx 를 도입 하여 탄성 상자 flex 구 조 를 배 워 야 합 니 다.js 부분 은 vue.js 와 유사 하 며 모두 데 이 터 를 연결 하고 js 의 dom 작업 을 간소화 합 니 다.이 두 가 지 는 좀 더 봐 야 겠 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기