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;
      }
    }
  }
};

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

相关推荐
粥里有勺糖2 分钟前
视野修炼-技术周刊第127期 | Valdi
前端·javascript·github
码上成长39 分钟前
Vue Router 3 升级 4:写法、坑点、兼容一次讲透
前端·javascript·vue.js
qq_398586542 小时前
浏览器中内嵌一个浏览器
前端·javascript·css·css3
abiao19812 小时前
如何在 VSCode 中创建 Vue 项目
ide·vue.js·vscode
西西西西胡萝卜鸡3 小时前
虚拟列表(Virtual List)组件实现与优化铁臂猿版(简易版)
前端·vue.js
宇余3 小时前
Unibest:新一代uni-app工程化最佳实践指南
前端·vue.js
*小雪3 小时前
uniapp写H5授权登录及分享,返回到目标页面
开发语言·javascript·uni-app
性野喜悲3 小时前
ts+uniapp小程序时间日期选择框(分开选择)
前端·javascript·vue.js
你不是我我4 小时前
【Java 开发日记】SQL 语句左连接右连接内连接如何使用,区别是什么?
java·javascript·数据库
一壶浊酒..4 小时前
请求签名(Request Signature)
javascript