基于Vue与Pinia实现页面Loading动画及请求拦截处理

作者:小布丁时间:2026-10-31 13:54:42浏览:182

本文详解如何在Vue项目中利用CSS与JavaScript实现页面加载时的Loading动画。通过Pinia管理全局加载状态,结合Axios拦截器在请求前后控制加载显示,并提供完整的组件化代码示例与jQuery传统实现方案,帮助开发者优化用户等待体验。

Vue项目中Loading动画的实现方案

Vue项目中Loading动画的实现方案

首先,定义用于展示Loading动画的CSS样式。

<style lang="less" scoped>
.loading {
  position: fixed;
  z-index: 999;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgba(0, 0, 0, 0.5);
}

.bg {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 104px;
  height: 104px;
  background: url(@/assets/img/home/loading-bg.png) 0 0 / 100% 100%;

  img {
    width: 70px;
    height: 70px;
    margin-bottom: 8px;
  }
}
</style>

其次,在HTML模板中引入对应的Loading元素节点。

<template>
  <div class="loading" v-if="mainStore.isLoading" @click="loadingClick">
    <div class="bg">
      <img src="@/assets/img/home/full-screen-loading.gif" alt="" />
    </div>
  </div>
</template>

接着,利用JavaScript逻辑在页面资源加载完毕后隐藏该Loading元素。

<script setup>
//1.引入main.js 
import useMainStore from "@/stores/modules/main";

const mainStore = useMainStore();

const loadingClick = () => {
  mainStore.isLoading = false;
};

本项目采用组件化架构,其中main.js主要承担数据存储与状态管理的职责,具体使用了Pinia库。

import { defineStore } from "pinia";

const useMainStore = defineStore("mian", {
     state: () => ({
            isLoading: true,
    )},
});
  
export default useMainStore;

在request.js文件中,对接口请求与响应过程进行拦截处理。

import axios from "axios";
import { BASE_URL, TIMEOUT } from "./config";
import useMainStore from "@/stores/modules/main";

const mainStore = useMainStore();

class HYRequest {
  constructor(baseURL, timeout = 10000) {
    this.instance = axios.create({
      baseURL,
      timeout,
    });
    // Loading加载显示与隐藏 第二种写法拦截请求
    //请求之前拦截
    this.instance.interceptors.request.use(
      (config) => {
        mainStore.isLoading = true;
        return config;
      },
      (err) => {
        return err;
      }
    );

    // 响应之前拦截
    this.instance.interceptors.response.use(
      (res) => {
        mainStore.isLoading = false;
        return res;
      },
      (err) => {
        mainStore.isLoading = false;
        return err;
      }
    );
  }

  request(config) {
    mainStore.isLoading = true;
    return new Promise((resolve, reject) => {
      this.instance
        .request(config)
        .then((res) => {
          resolve(res.data);
          // 第一种写法:Loading加载显示与隐藏
          // 请求结束后设置为false
          mainStore.isLoading = false;
        })
        .catch((err) => {
          reject(err);
          mainStore.isLoading = false;
        });
    });
  }

  get(config) {
    return this.request({ ...config, method: "get" });
  }

  post(config) {
    return this.request({ ...config, method: "post" });
  }
}
export default new HYRequest(BASE_URL, TIMEOUT);

JavaScript实现带进度条的Loading效果

补充:介绍使用JavaScript实现页面加载时带有进度条的Loading效果。

<html> 
<head> 
<script language="javascript" type="text/javascript" src="jquery-1.6.1.js">
</script> 
/** * 页面加载完成后显示页面 */ 
function showPage(){ $(&#39;#divPageLoading&#39;).remove(); 
$(&#39;#divPageBody&#39;).show(); } 
</head>
 <body onload="showPage();"> 
 <div id="divPageLoading"> 
 <img  src="pageloading.gif"   style="max-width:90%"/ alt="JS实现页面加载时带loading进度条" > 
 </div>
  <div id="divPageBody" style="display:none;"> …… 
 </div> 
 </body> 
 </html>

内容总结。

至此,关于利用JavaScript实现页面加载Loading动画的教程介绍完毕。如需了解更多JS页面Loading动画相关内容,建议查阅本站过往文章或继续浏览后续推荐内容,感谢大家的支持。

作者最新文章
相关文章
更多