const updateManager = wx.getUpdateManager() //版本更新管理器,用于管理小程序更新
updateManager.onCheckForUpdate(function (res) {
//请求新版本信息,返回对象若是 hasUpdate为true 则进行下一步
if (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: '新版本已经上线,请手动删除小程序从新添加',
})
})
}
})
复制代码
调试 在 工具-编译设置-新增编译条件小程序