vue中检测断网处理

在app.vue中定义一个变量,来记录是否显示断网

data() {
    return {
        network: true //默认有网
    };
},
// mounted中检测是否断网
mounted() {
    // 检测断网
    window.addEventListener("offline", () => {
        console.log("已断网");
        this.network= false;
    });
    window.addEventListener("online", () => {
        console.log("网络已连接");
        this.network= true;
    });
}
<!-- 断网显示图片替换内容 -->
<template>
  <div id="app">
    <!-- 断网显示的图片,使用转换工具转换成base64格式,才可以显示 -->
    <img
      class="nonet"
      v-if="!network"
      src="data:image/png;base64*******"
    />
    <router-view v-else />
  </div>
</template>
发布了76 篇原创文章 · 获赞 144 · 访问量 2974

猜你喜欢

转载自blog.csdn.net/qq_40745143/article/details/103909938