小程序版本更新代码记录

174 阅读1分钟

在APP.vue 文件中的onLaunch生命周期里添加如下代码

onLaunch: function() {

		// console.log('App Launch')
		 // 用户版本更新
		  if (uni.canIUse("getUpdateManager")) {
		    let updateManager = uni.getUpdateManager();
		   updateManager.onCheckForUpdate(function (res) {
		     // 请求完新版本信息的回调
		     console.log('APP'+res.hasUpdate);
		   });
		    updateManager.onUpdateReady(function (res) {
		      uni.showModal({
		        title: '更新提示',
		        content: '新版本已经准备好,是否重启应用?',
		        success(res) {
		          if (res.confirm) {
		            updateManager.applyUpdate();
		          }
		        }
		      });
		    
		    });
			updateManager.onUpdateFailed(function (res) {
			  // 新的版本下载失败
			  uni.showModal({
			     title: '已经有新版本了哟~',
			     content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
			     showCancel: false
			   });
			});
		  }

},