Vue HTML5+ APP热更新

<script>
export default {
  data() {
    return {
      version: "2.0.5",
    };
  },
  methods: {
    // 下载wgt文件
    downWgt(wgtUrl) {
      const that = this;
      plus.nativeUI.showWaiting("发现新版本,正在下载更新文件,请稍后...");
      plus.downloader
        .createDownload(
          wgtUrl,
          {
            filename: "_doc/update/",
          },
          function (d, status) {
            if (status == 200) {
              console.log("下载更新文件成功:" + d.filename);
              that.installWgt(d.filename); //安装wgt包
            } else {
              console.log("下载失败!");
              plus.nativeUI.alert("下载失败!");
              plus.nativeUI.alert(JSON.stringify(d));
              plus.nativeUI.alert(status);
            }
            plus.nativeUI.closeWaiting();
          }
        )
        .start();
    },
    installWgt(path) {
      plus.nativeUI.showWaiting("正在安装更新文件...");
      plus.runtime.install(
        path,
        {},
        function () {
          plus.nativeUI.closeWaiting();
          console.log("安装更新文件成功!");
          plus.nativeUI.alert("应用资源更新完成!", function () {
            plus.runtime.restart();
          });
        },
        function (e) {
          plus.nativeUI.closeWaiting();
          console.log("安装更新文件失败[" + e.code + "]:" + e.message);
          plus.nativeUI.alert("安装更新文件失败[" + e.code + "]:" + e.message);
          if (e.code == 10) {
            alert("请清除临时目录");
          }
        }
      );
    },
    plusReady() {
      const that = this;
      // plus.nativeUI.alert(plus.runtime.version);
      this.post("login/app_update", {
        version: this.version,
      }).then((res) => {
        const resData = res.data;
        if (resData.data.update === 1) {
          this.downWgt(resData.data.upgradeUrl);
        }
      });
    },

  },
  mounted() {
    this.post("login/app_update", {
      version: this.version,
    }).then((res) => {});
    if (window.plus) {
      this.plusReady();
    } else {
      document.addEventListener("plusready", this.plusReady, false);
    }
  },
};
</script>

更多内容参考uni-app 整包升级/更新方案

原文地址:https://www.cnblogs.com/aahan/p/14134059.html