在小程序app.js中配置
onLaunch() {
const updateManager = wx.getUpdateManager()
// UpdateManager.onCheckForUpdate(function listener) 监听向微信后台请求检查更新结果事件。微信在小程序每次启动(包括热启动)时自动检查更新,不需由开发者主动触发。
updateManager.onCheckForUpdate(function (res) {
// 请求完新版本信息的回调,有新版本res.hasUpdate=true,没有检测到新版本res.hasUpdate=false
// console.log(res.hasUpdate)
})
// 监听小程序有版本更新事件。客户端主动触发下载(无需开发者触发),下载成功后回调
updateManager.onUpdateReady(function () {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success: function (res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
}
}
})
})
// 监听小程序更新失败事件。小程序有新版本,客户端主动触发下载(无需开发者触发),下载失败(可能是网络原因等)后回调
updateManager.onUpdateFailed(function () {
// 新版本下载失败
wx.showModal({
title: '更新提示',
content: '更新失败,请手动刷新小程序后尝试',
})
})
}