VueApp 自动更新解决plus is not defined问题

一,今天用VueApp 做自动更新调用按照网上列子直接 Plus 打包编译后出现了plus is not defined 

 发现需要引用document.addEventListener("plusready",function(){}) 

代码如下

 

<script> export default { name: "looklive", components: { // eslint-disable-next-line vue/no-unused-components headcontrol, }, data() { return { path: "", installFlag: false, }; }, mounted() { //自动更新 this.getNativeVersion(); }, methods: { // 获取当前版本号 getNativeVersion() { let that = this; document.addEventListener("plusready", function () { plus.runtime.getProperty(plus.runtime.appid, function (inf) { that.nativeVersion = inf.version; that.checkUpdate(inf.version); localStorage.setItem("nativeVersion", inf.version); }); }); }, // 检查更新 checkUpdate(nativeVersion) { let that = this; const checkUrl = "http://119.23.211.81:8000/api/ip/GetAppVersion"; that.axios.get(checkUrl).then((result) => { // alert("服务区版本" + result.data.version); //alert(nativeVersion); if (!that.VersionContrast(nativeVersion, result.data.version)) that.downloadApk(result.data.url); }); }, //版本号对比 VersionContrast(curV, reqV) { // 当前版本号:curV;比较版本号:reqV if (curV && reqV) { let arr1 = curV.split("."), arr2 = reqV.split("."); let minLength = Math.min(arr1.length, arr2.length), position = 0, diff = 0; //依次比较版本号每一位大小,当对比得出结果后跳出循环(后文有简单介绍) while ( position < minLength && (diff = parseInt(arr1[position]) - parseInt(arr2[position])) == 0 ) { position++; } diff = diff != 0 ? diff : arr1.length - arr2.length; //若curV大于reqV,则返回true return diff >= 0; } }, // 下载apk文件 downloadApk(url) { let that = this; let watiting = plus.nativeUI.showWaiting("安装文件..."); let dtask = plus.downloader.createDownload(url, { method: "GET", retry: 0, }); dtask.addEventListener( "statechanged", function (task, status) { if (!dtask) { return; } switch (task.state) { case 1: break; case 2: break; case 3: var nowData = Math.floor( (task.downloadedSize * 100) / task.totalSize ); console.log(nowData); if (nowData % 10 === 0) { watiting.setTitle("已下载:" + nowData + "%"); if (nowData === 100) { watiting.toast("正在准备环境,请稍后!"); watiting.close(); } } break; case 4: // 安装apk资源包 plus.runtime.install( dtask.filename, {}, function () { plus.nativeUI.closeWaiting(); plus.nativeUI.alert("更新完成!", function () { // 更新完成后重启应用 plus.runtime.restart(); }); }, function (e) { plus.nativeUI.closeWaiting(); plus.nativeUI.toast("安装更新失败!"); } ); break; default: break; } }, false ); dtask.setRequestHeader("Access-Control-Allow-Origin", "*"); dtask.start(); }, }, }; </script>

 

效果如下

  


__EOF__

本文作者可乐加冰
本文链接https://www.cnblogs.com/zt199510/p/13879768.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   可乐_加冰  阅读(3186)  评论(1编辑  收藏  举报
编辑推荐:
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
阅读排行:
· 本地部署 DeepSeek:小白也能轻松搞定!
· 基于DeepSeek R1 满血版大模型的个人知识库,回答都源自对你专属文件的深度学习。
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 在缓慢中沉淀,在挑战中重生!2024个人总结!
· 大人,时代变了! 赶快把自有业务的本地AI“模型”训练起来!
点击右上角即可分享
微信分享提示