uniapp 微信小程序自动更新版本

uniapp yekong 1699℃

uniapp开发微信小程序时,经常遇到小程序发布了新版本,但是用户手机的app仍然是旧版本的,需要手动删除重新进入才可以。

解决办法

在App.vue onShow内加入以下代码:

const updateManager = uni.getUpdateManager();

    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      console.log(res.hasUpdate);
    });

    updateManager.onUpdateReady(function (res) {
      uni.showModal({
        title: '更新提示',
        content: '新版本已经准备好,是否重启应用?',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate();
          }
        }
      });

    });

    updateManager.onUpdateFailed(function (res) {
      // 新的版本下载失败
      console.log('download error')
      uni.showModal({
        title: '提示',
        content: '新版小程序下载失败\n请自行退出程序,手动卸载本程序,再运行',
        confirmText: "知道了"
      });
    });

添加编译模式

添加编译模式

选择下次编译时,模拟更新

选择下次编译时,模拟更新

效果

效果

效果2

转自:uni-app控制小程序版本更新;小程序自动更新版本;uni-app发布新版本后仍旧是老版本问题

喜欢 (0)