郑州web网站建设公司百度网站名称
小程序更新: uniapp小程序更新逻辑
uni.getUpdateManager()
方法 | 参数 | 说明 |
---|---|---|
onCheckForUpdate | callback | 当向小程序后台请求完新版本信息,会进行回调 |
onUpdateReady | callback | 当新版本下载完成,会进行回调 |
onUpdateFailed | callback | 当新版本下载失败,会进行回调 |
applyUpdate | 当新版本下载完成,调用该方法会强制当前小程序应用上新版本并重启 |
官方版本
const updateManager = uni.getUpdateManager();updateManager.onCheckForUpdate(function (res) {// 请求完新版本信息的回调console.log(res.hasUpdate);
});updateManager.onUpdateReady(function (res) {uni.showModal({title: '更新提示',content: '新版本已经准备好,是否重启应用?',success(res) {if (res.confirm) {// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启updateManager.applyUpdate();}}});});updateManager.onUpdateFailed(function (res) {// 新的版本下载失败
});
实际开发
//app.js
App({onLaunch() {this.update()},// 版本更新update() {const updateManager = wx.getUpdateManager()updateManager.onCheckForUpdate(function (res) {// 请求完新版本信息的回调if(res.hasUpdate) {// 新版本下载成功updateManager.onUpdateReady(function () {wx.showModal({title: '更新提示',content: '新版本已经准备好,请您重启应用,以确保正常使用。',success: function (res) {if (res.confirm) {// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启updateManager.applyUpdate()}}})})// 新版本下载失败updateManager.onUpdateFailed(function () {wx.showModal({title: '更新提示',content: '检测到了新版本,但是下载失败了~'})})}})}
})