1. 程式人生 > >微信小程式常用程式碼

微信小程式常用程式碼

1、請求

var app = getApp();
var that = this;
wx.request({
  url: app.globalData.host + '/getMoney',
  data: {
    token: app.globalData.token,
    id: app.globalData.id,
  },
  header: {
    'content-type': 'application/json' // 預設值
  },
  success: function (res) {
    console.log(res.data)
    that.setData({
      keyword: res.data,
    });
  }
})

2、保持縱橫比縮放圖片,只保證圖片的短邊能完全顯示出來

<image mode="aspectFill" mode="aspectFill" src="{{item['img_url']}}"></image>

3、轉跳頁面

wx.navigateTo({
  url: '/pages/index/chat/chat?live=' + JSON.stringify(that.data.live)
  + '&date=' + that.data.date
  + '&time=' + that.data.time
  + '&price=' + that.data.price
})
onLoad: function (options) {
  console.log(options)
  console.log(JSON.parse(options.live))
}
wx.navigateBack({
  delta: 1
})

4、提示框

wx.showModal({
  title: '提示',
  content: '直播時長小於十分鐘',
  showCancel:false,
})
wx.showModal({
  title: '提示',
  content: '這是一個模態彈窗',
  success: function(res) {
    if (res.confirm) {
      console.log('使用者點選確定')
    } else if (res.cancel) {
      console.log('使用者點選取消')
    }
  }
})

5、空列表站位提示

<view wx:if="{{chats.length==0}}" class="weui-loadmore weui-loadmore_line">
  <view class="weui-loadmore__tips weui-loadmore__tips_in-line">快來聊天吧</view>
</view>

6、bindtap傳值

<view bindtap="change1" data-type="App\\Live" data-id="{{live.id}}">修改</view>
console.log(options.currentTarget.dataset.id);

7、防止兩次點選操作間隔太快

var nowTime = new Date();
if (nowTime - this.data.tapTime < 1000) {
  return;
}
that.setData({ 'tapTime': nowTime });