微信小程序版本更新 提示

话不多说,直接上代码:

可以在app.js这个页面中onLoad()函数中 直接复制 updateAndRestartXcx 函数里面的方法!


// 小程序自动更新并重启
export function updateAndRestartXcx() {
  // 判断应用的 getUpdateManager 是否在当前版本可用

  // if(uni.canIUse('getUpdateManager')) {

    const updateManager = uni.getUpdateManager()

    updateManager.onCheckForUpdate(function (res) {
  
      // 请求完新版本信息的回调
        if(res.hasUpdate){
          updateManager.onUpdateReady(function (ress) {
            uni.showModal({
              title: '更新提示',
              content: '新版本已经准备好,是否重启应用?',
              success(res) {
                if (res.confirm) {
                  // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate()
                }
              }
            })
          })
        }else{
          updateManager.onUpdateFailed(function () {
            // 新版本下载失败
            uni.showModal({
                title: '提示',
                content: '请您删除当前小程序,重新打开小程序',
            })
          })
        }
    })
  // } else {
  //   // 提示用户在最新版本的客户端上体验
  //   uni.showModal({
  //     title: '温馨提示',
  //     content: '当前微信版本过低,可能无法使用该功能,请升级到最新版本后重试。'
  //   })
  // }
}

猜你喜欢

转载自blog.csdn.net/weixin_65478269/article/details/127008047