【小程序開發總結】微信小程序開發常用技術方法總結


1.獲取input的值
<input bindinput="bindKeyInput" placeholder="輸入同步到view中"/>
 
bindKeyInput: function(e) { this.setData({ inputValue: e.detail.value }) },
1.獲取標簽屬性的屬性值data-:
<button binTap="buy" data-productid="101"></button>
//JS調用: buy:function(e){ console.log(e.target.dataset.productid); //輸出結果:101 }
<view bindtap='upEwm' data-which='1' > </div>
//JS調用: buy:function(e){ console.log(e.currentTarget.dataset.which); //輸出結果:1 }
 
注意:data-productid 中的productid必須為小寫,駝峰式命名會undefined.
2.跳轉頁面說明:
(1)跳轉到無底部菜單頁面
wx.navigateTo({
url: '../help/help'
})
(2)跳轉到有底部菜單頁面
wx.switchTab({
url: '../index/index'
})
3.修改當前頁面全局變量:
this.setData({
mode: mode
})
4.常見加載及提示彈窗:
wx.showToast({
title: '錄音時間太短',
icon: 'loading',
mask: true,
duration: 800
})
wx.showToast({
title: '開始播放!',
icon: 'success',
duration: 1200
})
 
wx.showLoading({
title: '正在搶紅包',
mask: true
})
wx.showModal({
title: '提示',
content: res.errMsg,
showCancel: false,
success: function (res) {
}
});
//封裝可簡單調用,減少代碼量
  modal: function(title, content) {
    wx.showModal({
      title: title,
      content: content,
      showCancel: false,
    })
  }
5.全局變量,方法的調用
(1)app.js:
App({
  setConfig: {
url: 'https://redpack.topmdrt.com',
urlImg: 'http://oss-img.topmdrt.com',
urlMp: 'https://redpack-img.topmdrt.com'
  },
  onLaunch: function() {
wx.clearStorage();
    this.userLogin();
  },
  modal: function(title, content) {
    wx.showModal({
      title: title,
      content: content,
      showCancel: false,
    })
  },
 
})
調用方法:
//獲取應用實例
const app = getApp()
 
app.setConfig.url
app.modal('溫馨提示', '沒有余額');
6.小程序開發常見表單提交驗證:
 
formSubmit: function(e) {
if(e.detail.value.name==""){
warn = "請填寫您的姓名!";
}else if(e.detail.value.tel==""){
warn = "請填寫您的手機號!";
}else if(!(/^1(3|4|5|7|8)\d{9}$/.test(e.detail.value.tel))){
warn = "手機號格式不正確";
}else{
}
}
 
var regIdNo = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
if(!regIdNo.test(e.detail.value.code)){
  alert(‘身份證號填寫有誤‘);
  return false;
}
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM