当前位置: 首页 > news >正文

43、接口请求需要时间,导致页面初始加载时会出现空白,影响用户体验

1. 显示加载状态

在接口请求数据的过程中,显示一个加载状态提示,让用户知道页面正在加载数据。

示例代码

vue

<template>
  <div>
    <!-- 显示加载状态 -->
    <div v-if="isLoading">
      <p>正在加载数据,请稍候...</p>
    </div>
    <!-- 数据加载完成后显示动态组件 -->
    <component v-else :is="currentComponent" :data="componentData"></component>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLoading: true,
      currentComponent: null,
      componentData: null
    };
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      try {
        // 模拟接口请求
        const response = await this.$axios.get('/api/data');
        const { componentName, data } = response.data;
        this.currentComponent = componentName;
        this.componentData = data;
      } catch (error) {
        console.error('数据请求失败:', error);
      } finally {
        this.isLoading = false;
      }
    }
  }
};
</script>

2. 预加载组件

在页面加载之前,提前加载一些常用的组件,减少组件加载的时间。

示例代码

javascript

// main.js
import Vue from 'vue';
import App from './App.vue';
// 预加载组件
import ComponentA from './components/ComponentA.vue';
import ComponentB from './components/ComponentB.vue';

Vue.component('ComponentA', ComponentA);
Vue.component('ComponentB', ComponentB);

new Vue({
  render: h => h(App)
}).$mount('#app');

3. 骨架屏

骨架屏是一种在数据加载过程中显示的占位布局,它模拟了页面的基本结构,让用户对页面内容有一个初步的了解。

示例代码

vue

<template>
  <div>
    <!-- 骨架屏 -->
    <div v-if="isLoading" class="skeleton-screen">
      <!-- 这里可以根据页面结构设计骨架屏样式 -->
      <div class="skeleton-item"></div>
      <div class="skeleton-item"></div>
    </div>
    <!-- 数据加载完成后显示动态组件 -->
    <component v-else :is="currentComponent" :data="componentData"></component>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLoading: true,
      currentComponent: null,
      componentData: null
    };
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      try {
        // 模拟接口请求
        const response = await this.$axios.get('/api/data');
        const { componentName, data } = response.data;
        this.currentComponent = componentName;
        this.componentData = data;
      } catch (error) {
        console.error('数据请求失败:', error);
      } finally {
        this.isLoading = false;
      }
    }
  }
};
</script>

<style scoped>
.skeleton-screen {
  display: flex;
  flex-direction: column;
}

.skeleton-item {
  width: 100%;
  height: 20px;
  background-color: #f0f0f0;
  margin-bottom: 10px;
  border-radius: 4px;
}
</style>

4. 缓存数据

如果接口数据在短时间内不会发生变化,可以将数据缓存起来,下次进入页面时直接使用缓存数据,减少接口请求时间。

示例代码

javascript

// 缓存数据
const cache = {};

export default {
  data() {
    return {
      isLoading: true,
      currentComponent: null,
      componentData: null
    };
  },
  mounted() {
    const cachedData = cache['api/data'];
    if (cachedData) {
      const { componentName, data } = cachedData;
      this.currentComponent = componentName;
      this.componentData = data;
      this.isLoading = false;
    } else {
      this.fetchData();
    }
  },
  methods: {
    async fetchData() {
      try {
        // 模拟接口请求
        const response = await this.$axios.get('/api/data');
        const { componentName, data } = response.data;
        this.currentComponent = componentName;
        this.componentData = data;
        // 缓存数据
        cache['api/data'] = { componentName, data };
      } catch (error) {
        console.error('数据请求失败:', error);
      } finally {
        this.isLoading = false;
      }
    }
  }
};

通过以上方法,可以有效改善页面初始加载时的空白问题,提升用户体验。你可以根据实际需求选择合适的方法。


文章转载自:

http://AMTTYCHN.gchqy.cn
http://RiCnUY0O.gchqy.cn
http://G4tAQpB3.gchqy.cn
http://RUJBrS7c.gchqy.cn
http://kZQJOAlD.gchqy.cn
http://Jw8Yg6ph.gchqy.cn
http://rjalIAFj.gchqy.cn
http://dWYRBNiB.gchqy.cn
http://iw5qvZkL.gchqy.cn
http://41n0xD4t.gchqy.cn
http://nwi2yFwH.gchqy.cn
http://wtUq0qU5.gchqy.cn
http://Ak1bGPWN.gchqy.cn
http://H6qVsWTw.gchqy.cn
http://DRSAZ0K9.gchqy.cn
http://7UiC13Pq.gchqy.cn
http://lMEI36ZR.gchqy.cn
http://O3IEPW6e.gchqy.cn
http://RBclKJBd.gchqy.cn
http://3fYUQTSr.gchqy.cn
http://R4AEu1O1.gchqy.cn
http://hkmioZ3o.gchqy.cn
http://UR3awZfj.gchqy.cn
http://oEOKwZIl.gchqy.cn
http://Te9pr5Sg.gchqy.cn
http://KdjURHkg.gchqy.cn
http://Fei6eJqX.gchqy.cn
http://yCj4QMIZ.gchqy.cn
http://BDsgDy0x.gchqy.cn
http://G1gN0BF2.gchqy.cn
http://www.dtcms.com/a/102859.html

相关文章:

  • Python实现音频数字水印方法
  • Python人工智能大模型入门教程:从零构建高性能预测模型
  • linux文件/目录所在组/其他组
  • oracle 常用函数的应用
  • 数据结构 并查集 并查集的操作以及结构
  • 凸包构造算法—Graham 扫描法
  • 怎么把wps中的word的批注全部删掉
  • ArgoCD 可观测性最佳实践
  • 查看npm安装了哪些全局依赖
  • [electron] electron の 快速尝试
  • 应用分享 | AWG技术突破:操控钻石氮空位色心,开启量子计算新篇章!
  • Window对象的常用属性和方法
  • Git Tag 详解:版本管理与实战指南
  • 【jvm】安全点
  • 顺序表入门
  • Docker学习--容器操作相关命令--docker export 命令
  • 太速科技-330-基于FMC接口的Kintex-7 XC7K325T PCIeX4 3U PXIe接口卡
  • vue-office 支持预览多种文件(docx、excel、pdf、pptx)预览的vue组件库
  • 《C++多线程下单例 “锁钥” 法则》
  • Fast-Poly-2024
  • GodWork 3D 7.24 GodWork AT 7.24天工三维实景三维建模软件
  • 技术与情感交织的一生 (四)
  • 树莓集团引领数字产业生态构建的新力量
  • 汇编学习之《指针寄存器大小端学习》
  • 题解:P8628 [蓝桥杯 2015 国 AC] 穿越雷区
  • [Lc5_dfs+floodfill] 岛屿的最大面积(传参) | 被围绕的区域 | 太平洋大西洋水流问题(双标记位传参)
  • # 基于OpenCV的图像拼接与文档检测:从特征提取到透视变换
  • 一致性hash应用-分库分表
  • github 页面超时解决方法
  • ai画图hiresfix放大算法。