微信小程序 版本强制更新,检测到新版本重启小程序

实现 小程序版本强制更新,用户即可完成更新最新版本,检测到新版本重启小程序。

修改app.js 的 onLaunch方法

//app.js
App({
  onLaunch: function () {
    // 版本自动更新代码
    const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
      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: '请您删除当前小程序,到微信 “发现-小程序” 页,重新搜索“天助定餐饮”打开呦~',
        showCancel: false
      })
    })
  }
})

本地测试,新建编译条件,关键是测试版本更新的时候每次都要勾选“下次编译时模拟更新”

image.png

即可。

你可能感兴趣的:(版本更新小程序)