最好在第一版本加上
onLaunch () {
if (!wx.canIUse("getUpdateManager")) return;
let updateManager = wx.getUpdateManager();
// 获取全局唯一的版本更新管理器,用于管理小程序更新
updateManager.onCheckForUpdate(function(res) {
// 监听向微信后台请求检查更新结果事件
if (res.hasUpdate) {
updateManager.onUpdateReady(function() {
//当新版本下载完成,会进行回调
wx.showModal({
title: '更新提示',
content: '检测到有新版本,请单击确定更新小程序',
showCancel: false,
success: function(res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启小程序
updateManager.applyUpdate();
}
}
})
})
// 小程序有新版本,会主动触发下载操作(无需开发者触发)
updateManager.onUpdateFailed(function() {
//当新版本下载失败,会进行回调
wx.showModal({
title: '提示',
content: '检查到有新版本,但下载失败,请稍后尝试',
showCancel: false,
})
})
}
});
}