美文网首页
微信小程序之更新管理器wx.getUpdateManager()

微信小程序之更新管理器wx.getUpdateManager()

作者: hao_developer | 来源:发表于2022-09-06 16:50 被阅读0次

微信小程序在版本更新后,从上面标题栏进去,发现还是没有更新,存在缓存问题

解决办法-微信提供的wx.getUpdateManager()

onLaunch(){
  // console.log('是否进入onlanuch');
  this.checkUpdateVersion()  
},
 checkUpdateVersion() {
    const _that = this;
    //判断微信版本是否 兼容小程序更新机制API的使用
    if (wx.canIUse('getUpdateManager')) {
      //创建 UpdateManager 实例
      const updateManager = wx.getUpdateManager();
      console.log('是否进入模拟更新');
      //检测版本更新
      updateManager.onCheckForUpdate(function (res) {
        console.log('是否获取版本');
        if (res.hasUpdate) { // 请求完新版本信息的回调
          updateManager.onUpdateReady(function () { //监听小程序有版本更新事件
            //新的版本已经下载好,调用 applyUpdate 应用新版本并重启 ( 此处进行了自动更新操作)
            wx.showModal({
              title: '更新提示',
              content: '新版本已准备好,是否重启应用?',
              success: (res) => {
                if (res.confirm) {
                  updateManager.applyUpdate();
                }
              }
            })
          })
          updateManager.onUpdateFailed(function () {
            // 新版本下载失败
            wx.showModal({
              title: '已经有新版本喽~',
              content: '请您删除当前小程序,到微信 “发现-小程序” 页,重新搜索打开哦~',
              showCancel: false,
            })
          })
        }
      })
    } else {
      //此时微信版本太低(一般而言版本都是支持的)
      wx.showModal({
        title: '溫馨提示',
        content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。',
        showCancel: false,
      })
    }
  },

相关文章

网友评论

      本文标题:微信小程序之更新管理器wx.getUpdateManager()

      本文链接:https://www.haomeiwen.com/subject/seoinrtx.html