//app.jsApp({ onLaunch: function (options) { this.autoUpdate() }, autoUpdate : function(){ var self = this if(wx.canIUse('getUpdateManager')){ const updateManager = wx.getUpdateManager() // 检查小程序是否有新版本发布 updateManager.onCheckForUpdate(function(res){ // 请求完新版本的信息回调 if(res.hasUpdate){ wx.showModal({ title: '更新提示', content: '检查到新版本,是否下载新版本并重启小程序?', success:function(res){ if(res.confirm){ // 下载 self.downloadAndUpdate(updateManager) }else if(res.cancel){ // 点击取消,做强制更新操作 wx.showModal({ title: '温馨提示', content: '必须强制更新哦,旧版本无法正常使用', showCancel:false,//隐藏取消按钮 confirmText:'确定更新', success:function(res){ if(res.confirm){ // 再次调用下载,并重启 self.downloadAndUpdate(updateManager) } } }) } } }) } }) }else{ // 增加用户体验,给出友好提示 wx.showModal({ title: '提示', content: '当前微信版本过低,无法使用该功能,请升级后重试。' }) } }, /** * 下载新版本并重启 */ downloadAndUpdate: function (updateManager) { var self = this wx.showLoading(); // 监听小程序有版本更新事件,客户端主动触发 updateManager.onUpdateReady(function(){ wx.hideLoading(); // 新版本下载好,调用applyUpdate updateManager.applyUpdate() }) // 监听小程序更新失败事件 updateManager.onUpdateFailed(function(){ wx.showModal({ title: '已经有新版了哦', content: '请你删除当前小程序,进行升级哦' }) }) }})用开发工具测试的时候,需要添加编译模式
并勾选如下复选框
内容参考 https://www.jb51.net/article/162673.htm














