1.生命周期
- 生命周期如下图:
-事例代码:
onLaunch: function () {
console.log('index---------onLaunch()')
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
},
onLoad: function () {
console.log('index---------onLoad()')
var that = this;
that.showLoading();
that.saveLocation();
wx.getLocation({
type: 'gcj02',
success: function (res) {
console.log("定位成功")
wx.hideLoading()
that.setData({
hasLocation: true,
longitude: res.longitude,
latitude: res.latitude,
scale: 40
})
wx.openLocation({
latitude: latitude,
longitude: longitude,
scale: scale
})
},
fail: function (e) {
console.log("定位失败")
wx.hideLoading()
},
})
},
showLoading: function () {
wx.showLoading({
title: '定位中',
mask: true,
})
setTimeout(function () {
wx.hideLoading()
}, 3000)
},
saveLocation: function () {
var that = this;
console.log("进入缓存");
console.log(that.data.latitude + "," + that.data.longitude);
try {
wx.setStorageSync('lat', that.data.latitude);
wx.setStorageSync('lon', that.data.longitude);
} catch (e) {
}
},
onShow: function () {
console.log('index---------onShow()')
},
onReady: function () {
console.log('index---------onReaday()');
},
onHide: function () {
console.log('index---------onHide()')
},
onUnload: function () {
console.log('index---------onUnload')
}