跟前端那套差不多,這裏積累一些常用的知識點(持續更新)
1. .js基本架構如下:
Page({
data: { a:'',b:''},//數據,可綁定前端 比如:<p>{{a}}</p>
f: function(e){ //自定義回調事件,可綁定前端標籤 比如:<button bindtap="f">text</button>
//變量定義
var v=...;
//調用API //小程序的API參數可以是變量 有的也可能是事件 文檔裏把參數稱爲屬性!
wx.XXX({
參數1:'',
參數2:'',
參數3:function(){//事件裏面也可以再調用API
...;
wx.XXXX({
參數1:'',...
});
}
});
},
})
2. .js常用的wxAPI
官方API文檔:https://developers.weixin.qq.com/miniprogram/dev/api/
1. 跳轉到某頁面 : wx.navigateTo({ url:'路徑' })
2. 發起Http請求到後臺:wx.request({})
其中屬性 header: { 'content-type': 'application/x-www-form-urlencoded' }, 要這樣寫
3. 消息提示窗 wx.showToast({})
3. 一些坑點
1. this.setData()在回調裏的回調會失效,得先var that=this; 再用that.setData() 因爲this進行回調之後不再是原來那個this
4.數組的使用、假設有個數組長下面這樣
data: {
dayStyle: [
{ month: 'current', day: new Date().getDate(), color: 'red', background: '#AAD4F5' },
{ month: 'current', day: new Date().getDate(), color: 'white', background: '#AAD4F5' },
{ month: 'current', day: 2, color: 'yellow', background: '#AAD4F5' },
]
},
4.1取data中的數組副本 var now = this.data.dayStyle;
4.2遍歷數組 for(var i=0;i<arr.length;i++){.....}
4.3數組添加 now.push({month:'',day:...,.....})
4.4將data中的數組更新 this.setData({ dayStyle: now, })
這個鏈接很全面 https://www.jianshu.com/p/185efaea230c