明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
  博客园  :: 首页  :: 管理

微信小程序 版本更新 强制更新新版本

Posted on 2024-04-18 10:16  且行且思  阅读(32)  评论(0编辑  收藏  举报

在点击小程序发布的时候选择,版本升级 选项

 

之前用户使用过的再打开小程序页面就会弹出升级弹窗modal

在utils.js中声明版本更新方法

 

// static/utils/index.js
 
export function checkUpdateVersion() {
  //创建 UpdateManager 实例
  const updateManager = uni.getUpdateManager();
  //检测版本更新
  updateManager.onCheckForUpdate(function(res) {
    // 请求完新版本信息的回调
    if (res.hasUpdate) {
      //监听小程序有版本更新事件
      updateManager.onUpdateReady(function() {
        uni.showModal({
          title: '更新提示',
          content: '新版本已经准备好,是否重启应用?',
          success(res) {
            if (res.confirm) {
              // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
              updateManager.applyUpdate();
            }
          }
        })
      })
 
      updateManager.onUpdateFailed(function() {
        // 新版本下载失败
        uni.showModal({
          title: '已经有新版本咯~',
          content: '请您删除当前小程序,到微信 “发现-小程序” 页,重新搜索打开呦~',
        })
      })
    }
  })
}

 

在App.vue调用更新方法,在onLaunch声明周期调用,版本更新

//App.vue
<script setup>
    import {
        checkUpdateVersion
    } from '@/utils/updatemanager'; //引入须在onLaunch的前端。
    import {
        onShow,
        onLaunch,
    } from "@dcloudio/uni-app" onLaunch: function() { checkUpdateVersion(); //版本更新 }