小程序之版本更新

1)微信开发者工具上可以通过「编译模式」下的「下次编译模拟更新」开关来调试

2)小程序开发版/体验版没有「版本」概念,所以无法在开发版/体验版上测试更版本更新情况

在App.js里面添加

onLaunch () {

  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: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~'

      })

    })

    }

  })

  }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。