前言:
技术支持:
1.判断小程序的API,回调,参数,组件等是否在当前版本可用
wx.canIUse();
2.获取全局唯一的版本更新管理器,用于管理小程序更新
wx.getUpdateManager();
3.监听向微信后台请求检查更新结果事件。微信在小程序每次启动(包括热启动)时自动检查更新,不需由开发者主动触发。
UpdateManager.onCheckForUpdate();
4.监听小程序有版本更新事件。客户端主动触发下载(无需开发者触发),下载成后回调
UpdateManager.onUpdateReady();
5.强制小程序重启并使用新版本。在小程序新版本下载完成后
UpdateManager.applyUpdate()
6.监听小程序更新失败事件。小程序有新版本,客户端主动触发下载(无需开发者触发),下载失败(可能是网络原因等)后回调
UpdateManager.onUpdateFailed();
链接----微信小程序更新
具体代码如下:
onLaunch(options) {
this.getNew();
},
getNew(){
if (wx.canIUse('getUpdateManager')) {
const updateManager = wx.getUpdateManager()
updateManager.onCheckForUpdate(function (res) {
// console.log(res, "是否有新版本")
if (res.hasUpdate) {
updateManager.onUpdateReady(function () {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success: function (res) {
if (res.confirm) {
updateManager.applyUpdate()
}
}
})
})
updateManager.onUpdateFailed(function () {
wx.showModal({
title: '已经有新版本了哟~',
content: '新版本已经上线,请您删除当前小程序,重新搜索打开'
})
})
}
})
} else {
wx.showModal({
title: '提示',
content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
})
}
},