微信小程序開發過程中,我們在版本迭代后,客戶端並不能觸發這個自動更新,需要清掉小程序后重新搜索進入才能獲取到最新的小程序,但這個是用戶所不能感知到的操作,故需要提醒用戶如何去get到最新的版本:
直接在app.js中的onLaumch鈎子函數中添加以下代碼即可:
onLaunch: function (){ if (wx.canIUse('getUpdateManager')) { const updateManager = wx.getUpdateManager() updateManager.onCheckForUpdate(function (res) { console.log('onCheckForUpdate====', res) // 請求完新版本信息的回調 if (res.hasUpdate) { console.log('res.hasUpdate====') updateManager.onUpdateReady(function () { wx.showModal({ title: '更新提示', content: '新版本已經准備好,是否重啟應用?', success: function (res) { console.log('success====', res) // res: {errMsg: "showModal: ok", cancel: false, confirm: true} if (res.confirm) { // 新的版本已經下載好,調用 applyUpdate 應用新版本並重啟 updateManager.applyUpdate() } } }) }) updateManager.onUpdateFailed(function () { // 新的版本下載失敗 wx.showModal({ title: '已經有新版本了喲~', content: '新版本已經上線啦~,請您刪除當前小程序,重新搜索打開喲~' }) }) } }) } // this.UserLogin(); }
來源:https://blog.csdn.net/kirinlau/article/details/89022913