UpdateManager

UpdateManager 对象,用来管理更新,可通过 Taro.getUpdateManager 接口获取实例。

Tips

  • 微信开发者工具上可以通过「编译模式」下的「下次编译模拟更新」开关来调试
  • 小程序开发版/体验版没有「版本」概念,所以无法在开发版/体验版上测试更版本更新情况

支持情况:UpdateManager - 图1 UpdateManager - 图2 UpdateManager - 图3

参考文档

方法

applyUpdate

强制小程序重启并使用新版本。在小程序新版本下载完成后(即收到 onUpdateReady 回调)调用。

支持情况:UpdateManager - 图4 UpdateManager - 图5 UpdateManager - 图6

参考文档

  1. () => void

onCheckForUpdate

监听向微信后台请求检查更新结果事件。微信在小程序冷启动时自动检查更新,不需由开发者主动触发。

支持情况:UpdateManager - 图7 UpdateManager - 图8 UpdateManager - 图9

参考文档

  1. (callback: OnCheckForUpdateCallback) => void
参数 类型 说明
callback OnCheckForUpdateCallback 向微信后台请求检查更新结果事件的回调函数

onUpdateReady

监听小程序有版本更新事件。客户端主动触发下载(无需开发者触发),下载成功后回调

支持情况:UpdateManager - 图10 UpdateManager - 图11 UpdateManager - 图12

参考文档

  1. (callback: (res: TaroGeneral.CallbackResult) => void) => void
参数 类型 说明
callback (res: TaroGeneral.CallbackResult) => void 小程序有版本更新事件的回调函数

onUpdateFailed

监听小程序更新失败事件。小程序有新版本,客户端主动触发下载(无需开发者触发),下载失败(可能是网络原因等)后回调

支持情况:UpdateManager - 图13 UpdateManager - 图14 UpdateManager - 图15

参考文档

  1. (callback: (res: TaroGeneral.CallbackResult) => void) => void
参数 类型 说明
callback (res: TaroGeneral.CallbackResult) => void 小程序更新失败事件的回调函数

参数

OnCheckForUpdateCallback

向微信后台请求检查更新结果事件的回调函数

  1. (result: OnCheckForUpdateResult) => void
参数 类型
result OnCheckForUpdateResult

OnCheckForUpdateResult

参数 类型 说明
hasUpdate boolean 是否有新版本

示例代码

  1. const updateManager = Taro.getUpdateManager()
  2. updateManager.onCheckForUpdate(function (res) {
  3. // 请求完新版本信息的回调
  4. console.log(res.hasUpdate)
  5. })
  6. updateManager.onUpdateReady(function () {
  7. wx.showModal({
  8. title: '更新提示',
  9. content: '新版本已经准备好,是否重启应用?',
  10. success: function (res) {
  11. if (res.confirm) {
  12. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  13. updateManager.applyUpdate()
  14. }
  15. }
  16. })
  17. })
  18. updateManager.onUpdateFailed(function () {
  19. // 新版本下载失败
  20. })

API 支持度

API 微信小程序 H5 React Native
UpdateManager ✔️
UpdateManager.applyUpdate ✔️
UpdateManager.onCheckForUpdate ✔️
UpdateManager.onUpdateReady ✔️
UpdateManager.onUpdateFailed ✔️