【前端】Vue项目:旅游App-(19)loading:网络请求时显示loading效果

news2024/10/6 19:17:07

文章目录

    • 目标
    • 过程与代码
      • loading效果
      • mainStore控制loading显示
      • 点击蒙板取消网络请求
      • 在网络请求处添加对loading的控制
    • 效果
    • 总代码
      • 修改或添加的文件
      • loading.vue
      • service/request/index
      • store/modules/main
      • App.vue
    • 参考

本项目博客总结:【前端】Vue项目:旅游App-博客总结

目标

在发送网络请求的时候显示loading:

在这里插入图片描述

过程与代码

loading效果

本App中任何一个地方发送网络请求都要显示loading效果,因此我们要把它封装在一个组件中。且在App.vue中引入。

css代码解释,此做法可以:

  • 让明确宽高的盒子垂直水平居中
  • 让无宽高的盒子填满父容器
  • 这里是第二种
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;

loading总代码:

<template>
    <div class="loading">
        <div class="bg">
            <img src="@/assets/img/home/full-screen-loading.gif" alt="">
        </div>
    </div>
</template>

<script setup>

</script>

<style lang="less" scoped>
.loading {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;

    // 蒙板颜色
    background-color: rgba(0, 0, 0, .2);

    // loading的效果会在任何效果之上
    z-index: 999;

    // 垂直水平居中
    display: flex;
    justify-content: center;
    align-items: center;

    .bg {
        background: url(@/assets/img/home/loading-bg.png) 0 0 /100% 100%;
        width: 104px;
        height: 104px;

        display: flex;
        justify-content: center;
        align-items: center;

        img {
            width: 70px;
            height: 70px;
            margin-bottom: 9px;
        }
    }

}
</style>

效果:背景有点灰色的地方就是蒙板

在这里插入图片描述

mainStore控制loading显示

我们的需求:在发送网络请求(简单来说就是加载数据)的时候显示loading。这样更加用户友好。

显然,App中很多页面都有可能要加载数据,因此我们需要把loading显示的控制放在一个地方统一管理。不难想到要放到store的mainStore中管理(上篇管理整个App时间设置的地方)。

useMainStore:

isLoading:false

html:

<div class="loading" v-if="mainStore.isLoading">

js:

import useMainStore from '../../store/modules/main';

const mainStore=useMainStore()

点击蒙板取消网络请求

这是一个常见的效果。

html:

<div class="loading" v-if="mainStore.isLoading" @click="loadingClick()">

js:

function loadingClick() {
    mainStore.isLoading = false
}

在网络请求处添加对loading的控制

我们的需求:在发送网络请求的时候显示loading。

而我们在代码中封装了网络请求的功能,很显然,我们可以在request中添加在对loading的控制,如:

request(config) {
 loadingStore.changeLoading(true);
 mainStore.isLoading = true
 return new Promise((resolve, reject) => {
   this.instance
     .request(config)
     .then((res) => {
       resolve(res.data);
       mainStore.isLoading = false
     })
     .catch((err) => {
       console.log("request err:", err);
       reject(err);
       mainStore.isLoading = false
     })
     .finally(() => {
       loadingStore.changeLoading(false);
     });
 });

在这里插入图片描述
在发送请求时显示loading,发送完或发送完都取消显示。

但是不推荐这样做,我们希望一个方法只做一个功能。这里推荐使用拦截器

拦截器 | Axios 中文文档 | Axios 中文网 (axios-http.cn)
axios 请求拦截器&响应拦截器 - 简书 (jianshu.com)

注意,拦截器是绑定在实例上的。我们要把它写在构造函数里。

constructor(baseURL) {
  this.instance = axios.create({
    baseURL,
    timeout: TIMEOUT,
  });

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

效果

发送网络请求时会显示loading,数据加载完后就消失。

总代码

修改或添加的文件

在这里插入图片描述

loading.vue

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>

<script setup>
import useMainStore from '../../store/modules/main';

const mainStore = useMainStore()

function loadingClick() {
    mainStore.isLoading = false
}
</script>

<style lang="less" scoped>
.loading {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;

    // 蒙板颜色
    background-color: rgba(0, 0, 0, .2);

    // loading的效果会在任何效果之上
    z-index: 999;

    // 垂直水平居中
    display: flex;
    justify-content: center;
    align-items: center;

    .bg {
        background: url(@/assets/img/home/loading-bg.png) 0 0 /100% 100%;
        width: 104px;
        height: 104px;

        display: flex;
        justify-content: center;
        align-items: center;

        img {
            width: 70px;
            height: 70px;
            margin-bottom: 9px;
        }
    }

}
</style>

service/request/index

在网络请求的代码中添加请求拦截器和响应拦截器。

import axios from "axios";

import { useLoadingStore } from "@/store/modules/loading";
import useMainStore from "../../store/modules/main";
import { baseURL, TIMEOUT } from "./config";

const loadingStore = useLoadingStore();
const mainStore = useMainStore()

class HYRequest {
  constructor(baseURL) {
    this.instance = axios.create({
      baseURL,
      timeout: TIMEOUT,
    });

    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) {
    loadingStore.changeLoading(true);
    return new Promise((resolve, reject) => {
      this.instance
        .request(config)
        .then((res) => {
          resolve(res.data);
        })
        .catch((err) => {
          console.log("request err:", err);
          reject(err);
        })
        .finally(() => {
          loadingStore.changeLoading(false);
        });
    });
  }

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

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

export default new HYRequest(baseURL);

store/modules/main

添加isLoading来控制loading的显示。

import { defineStore } from "pinia"

const startDay = new Date()
const endDay = new Date()
endDay.setDate(startDay.getDate() + 1)

const useMainStore = defineStore('main', {
    state: () => ({
        token:'',

        startDay:startDay,
        endDay:endDay,

        isLoading:false,
    }),
    actions: {

    }
})

export default useMainStore

App.vue

添加loading组件。

<template>
    <div class="app">
        <router-view/>
        <tab-bar v-if="!$route.meta.hideTabBar"></tab-bar>
        <loading/>
    </div>
</template>

<script setup>

import tabBar from './components/tab-bar/tab-bar.vue';
import loading from './components/loading/loading.vue';

import { useRoute } from 'vue-router'

const route=useRoute()

</script>

<style lang="less" scoped>

</style>

在这里插入图片描述

参考

css中 top, right, bottom, left设置为0有什么用?它和width:100%和height:100%有什么区别?

CSS Backgrounds(背景) | 菜鸟教程 (runoob.com)

拦截器 | Axios 中文文档 | Axios 中文网 (axios-http.cn)

axios 请求拦截器&响应拦截器 - 简书 (jianshu.com)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/195480.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

自动化运维|云原生架构下的产品自动化发布、快速部署和持续交付实战之路

自动化运维|云原生架构下的产品自动化发布、快速部署和持续交付实战之路。 1.背景介绍 CI/CD是一种通过在应用开发阶段引入自动化来频繁向客户交付应用的方法。CI/CD 的核心概念是持续集成、持续交付和持续部署。作为一种面向开发和运维团队的解决方案&#xff0c;CI/CD 主要针…

只做笔记有必要买apple pencil吗?平价电容笔排行榜

如今国内的电容笔&#xff0c;牌子也越来越多了&#xff0c;苹果原装电容笔虽然性能不错&#xff0c;但价格也非常昂贵&#xff0c;一般人是买不起的。现在市面上有很多可以取代原来的苹果电容笔的平替电容笔。下面&#xff0c;我给大家推荐几款电容笔&#xff0c;好用而且价格…

Java 文件IO操作基础,File,FileInputStream,FileOutputStream

什么是文件 有个时候我们程序中的数据不会直接保存&#xff0c;一旦程序断电&#xff0c;数据将不会存在&#xff0c;如果我们想要我们程序中的数据永久的保存起来&#xff0c;所以&#xff0c;java的文件io显得非常重要。 什么是文件流 创建文件 如下相关的文件io方法 new F…

【教程】Wireshark抓取抖音直播的OBS推流地址和密钥

真不用花钱去买提取推流码的软件。。。自己提取很简单的。 简单记录一下&#xff0c;以备自己需要时候忘了怎么搞。 1、电脑开热点&#xff0c;手机连接热点&#xff1b; 2、电脑安装wireshark软件&#xff0c;并选择WLAN或者以太网接口&#xff1b; 3、wireshark筛选rtmpt&am…

Kubernetes持久化Events到sentry

背景 Kubernetes中的事件最终还是存储在etcd中&#xff0c;默认情况下只保存1个小时&#xff0c;由于etcd并不支持一些复杂的分析操作&#xff0c;默认Kubernetes只提供了非常简单的过滤方式&#xff0c;比如通过Reason、时间、类型等。同时这些事件只是被动的存在etcd中&…

CAPL(vTESTStudio) - DOIP - UDP发送_03

继UDP接收的介绍完成后,今天我们介绍下UDP发送的函数,这里我们将我自主开发的函数整体都会介绍个大家,一般能够完成大家日常脚本开发中90%以上使用发送UDP的数据函数,绝对干货满满。 UDP发送 一、参数定义 无论DoIP发送报文的UDP还是TCP函数,亦或是CAN、CANFDLIN的发送函…

Redux了解及应用(三)

React - redux 使用&#xff08;由浅入深&#xff09;&#xff1a;https://blog.csdn.net/Jie_1997/article/details/128078971 这篇文章总结的很棒&#xff01;&#xff01;&#xff01;了解redux及应用直接看这篇文章即可 备注&#xff1a;第五节的第三小节&#xff0c;容器…

学习驱动的复杂软件符号执行

原文来自微信公众号“编程语言Lab”&#xff1a;学习驱动的复杂软件符号执行搜索关注“编程语言Lab”公众号&#xff08;HW-PLLab&#xff09;获取编程语言更多技术内容&#xff01;欢迎加入编程语言社区 SIG-编程语言测试&#xff0c;了解更多编程语言测试相关的技术内容。加入…

元宇宙:有人追捧,就会有人抵触

或许&#xff0c;直到现在&#xff0c;我们依然无法否认元宇宙即将对我们的生产和生活产生的深刻影响。即使是在它遭遇巨大的不确定性的大背景下&#xff0c;依然如此。 有人追捧&#xff0c;便有人抵触。元宇宙商用的止步不前&#xff0c;元宇宙技术的难以突破……几乎都是这…

为什么不进行穷举测试?

本章主要介绍不对所有可能性进行测试的原因&#xff0c;对于经理和测试人员&#xff0c;都应该了解测试是一种采样过程&#xff0c;需要了解采样给测试所带来的风险。 1、可进行测试的数目是无限的 如果不能查看代码内部逻辑&#xff0c;可输入的测试用例是无限的。当然还有在不…

第30章 分布式缓存强制删除触发器的触发调试

1 Services.Users.Caching.RoleCacheEventConsumer using Core.Caching; using Core.Domain.Users; using Services.Caching; namespace Services.Users.Caching { /// <summary> /// 摘要&#xff1a; /// 通过该类中的方法成员&#xff0c;在角色实体的1个实例…

Linux —— 文件系统概述、软硬链接与动静态库

目录 1.文件系统概述 1.1磁盘的基本存储结构 1.2磁盘的基本逻辑结构 1.3操作系统中的文件系统 1.4文件系统如何对磁盘进行管理 2.软链接、硬链接 2.1软链接 2.2硬链接 2.3目录的硬链接数 3.静态库和动态库 3.1静态库的制作 3.2静态库的使用 3.3动态库的制作 3.4动态…

年薪50k大佬带你五分钟学会接口自动化测试框架

今天&#xff0c;我们来聊聊接口自动化测试是什么&#xff1f;如何开始&#xff1f;接口自动化测试框架怎么做&#xff1f;自动化测试自动化测试&#xff0c;这几年行业内的热词&#xff0c;也是测试人员进阶的必备技能&#xff0c;更是软件测试未来发展的趋势。特别是在敏捷模…

分布式请求链路跟踪-SpringCloud Sleuth

文章目录1.概述1.1.为什么会出现这个技术&#xff1f; 需要解决哪些问题?1.2.是什么?1.3.如何解决问题?2.搭建链路监控步骤2.1.zipkin2.2.服务提供者2.3.服务消费者&#xff08;调用方&#xff09;2.4.测试1.概述 1.1.为什么会出现这个技术&#xff1f; 需要解决哪些问题?…

力扣刷题记录——1108. IP 地址无效化、1281. 整数的各位积和之差 次数 、1295. 统计位数为偶数的数字、1394. 找出数组中的幸运数

本专栏主要记录力扣的刷题记录&#xff0c;备战蓝桥杯&#xff0c;供复盘和优化算法使用&#xff0c;也希望给大家带来帮助&#xff0c;博主是算法小白&#xff0c;希望各位大佬不要见笑&#xff0c;今天要分享的是——《力扣刷题记录——1108. IP 地址无效化、1281. 整数的各位…

手撕排序算法(一)——插入排序

排序的概念及意义本章内容我们采用C语言完成代码。排序的概念我们先来了解一下基础概念&#xff1a;排序&#xff1a;所谓排序&#xff0c;就是使一串记录&#xff0c;按照其中的某个或某些关键字的大小&#xff0c;递增或递减的排列起来的操作。稳定性&#xff1a;假定在待排序…

cin关闭流同步的利弊与cout的endl使用(超时问题)

重要&#xff1a;1&#xff1a;比如print&#xff0c;scanf&#xff0c;gets()&#xff0c;pus()&#xff0c;getchar()不要与cin&#xff0c;cout共用2&#xff1a;cout中不要使用endl每次使用endl&#xff0c;都要flush缓冲区&#xff0c;造成大量时间耗费。推荐cout <<…

springBoot国际化的一种方式

引言&#xff1a; 当我们的应用面向不同国家用户时&#xff0c;根据不同的locale返回不同的语言信息的国际化功能就显得有必要了。一般来说国际化主要表现在前端用户界面上&#xff0c;在现在前后端分离的背景下&#xff0c;前端页面的国际化交由前端代码独立完成&#xff1b;少…

只用两行代码做个表白二维码,赶快送给你心目中那个她吧♥(๑> ₃ <)♥

上一篇&#xff1a;教你一招完美解决 pptx 库安装失败的问题 今天有同事给我说&#xff1a;女朋友生日快到了&#xff0c;想用Python给她写个表白二维码&#xff0c;然后印在买的衣服上送给她。这么特别的生日礼物&#xff0c;博主还是第一次听到&#xff0c;不得不说&#xff…

Linux-make/Makefile

一、了解make/Makefile对于make/Makefile首先我们需要了解make是一条命令&#xff1b;Makefile是一个文件。make是一个命令&#xff0c;可以执行某条指令。这个我们理解&#xff0c;那Makefile是一个文件&#xff0c;那这个文件是干什么用的呢&#xff1f;这个文件内部一共包含…