onLaunch (options) {
const updateManager = wx.getUpdateManager()
updateManager.onUpdateReady(function () {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
})
updateManager.onUpdateFailed(function () {
// 新的版本下载失败
wx.showModal({
title: '更新提示',
content: '新版本下载失败',
showCancel: false
})
})
}
进行封装调用
/* 小程序监听更新信息 */
export function checkUpdateVersion() {
//创建 UpdateManager 实例
const updateManager = wx.getUpdateManager();
//检测小程序版本更新
updateManager.onCheckForUpdate((res) => {
// 请求完新版本信息的回调
if (res.hasUpdate) {
//监听小程序有版本更新事件
updateManager.onUpdateReady(() => {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
ok(res) {
// 新的版本下载完毕,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate();
}
})
})
//监听小程序更新失败事件
updateManager.onUpdateFailed(() => {
// 新版本下载失败
wx.showModal({
title: '已经有新版本咯~',
content: '请在微信 “发现-小程序” 页删除当前小程序,重新通过小程序名称搜索打开使用最新版本',
})
})
}
})
}