Vue入门(11)axios请求拦截器实现数据加载出来之前loading效果

axios请求拦截器实现数据加载出来之前loading效果

实现思路:用CSS3写出一个loading动画,默认隐藏。在请求拦截器里让动画显示出来,再在响应拦截器里隐藏动画。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="node_modules/vue/dist/vue.min.js"></script>
    <script src="node_modules/axios/dist/axios.js"></script>
    <style type="text/css">
    .box {
        width: 100%;
        padding: 3%;
        box-sizing: border-box;
        overflow: hidden
    }

    .box .loader {
        width: 30%;
        float: left;
        height: 200px;
        margin-right: 3%;
        box-sizing: border-box;
        display: flex;
        align-items: center;
        justify-content: center
    }

    @-webkit-keyframes loading-3 {
        50% {
            transform: scale(.4);
            opacity: .3
        }
        100% {
            transform: scale(1);
            opacity: 1
        }
    }

    .loading-3 {
        position: relative
    }

    .loading-3 i {
        display: block;
        width: 15px;
        height: 15px;
        border-radius: 50%;
        background-color: #333;
        position: absolute
    }

    .loading-3 i:nth-child(1) {
        top: 25px;
        left: 0;
        -webkit-animation: loading-3 1s ease 0s infinite
    }

    .loading-3 i:nth-child(2) {
        top: 17px;
        left: 17px;
        -webkit-animation: loading-3 1s ease -.12s infinite
    }

    .loading-3 i:nth-child(3) {
        top: 0;
        left: 25px;
        -webkit-animation: loading-3 1s ease -.24s infinite
    }

    .loading-3 i:nth-child(4) {
        top: -17px;
        left: 17px;
        -webkit-animation: loading-3 1s ease -.36s infinite
    }

    .loading-3 i:nth-child(5) {
        top: -25px;
        left: 0;
        -webkit-animation: loading-3 1s ease -.48s infinite
    }

    .loading-3 i:nth-child(6) {
        top: -17px;
        left: -17px;
        -webkit-animation: loading-3 1s ease -.6s infinite
    }

    .loading-3 i:nth-child(7) {
        top: 0;
        left: -25px;
        -webkit-animation: loading-3 1s ease -.72s infinite
    }

    .loading-3 i:nth-child(8) {
        top: 17px;
        left: -17px;
        -webkit-animation: loading-3 1s ease -.84s infinite
    }

</style>
</head>
<body>
    <div id="app">
        <button @click="handler">按钮</button>
        <h1>这是请求回来的数据{{msg}}</h1>
        <div class="box" v-show="isShow">
            <div class="loader">
                <div class="loading-3">
                    <i></i>
                    <i></i>
                    <i></i>
                    <i></i>
                    <i></i>
                    <i></i>
                    <i></i>
                    <i></i>
                </div>
            </div>
        </div>
    </div>
    <script>
        new Vue({
            el : "#app",
            data(){
                return {
                    msg : "",
                    isShow : false
                }
            },
            methods : {
                handler(){
                    // 添加请求拦截器
                    axios.interceptors.request.use((config)=>{
                        // 在发送请求之前做些什么
                        console.log(config);
                        this.isShow = true
                        return config;
                    }, function (error) {
                        // 对请求错误做些什么
                        return Promise.reject(error);
                    });

                    // 添加响应拦截器
                    axios.interceptors.response.use((response)=> {
                        // 对响应数据做点什么
                        console.log(response);
                        this.isShow = false
                        return response;
                    }, function (error) {
                        // 对响应错误做点什么
                        return Promise.reject(error);
                    });


                    axios.get("xxx.com/1/report/index")
                    .then((res) => {
                        console.log(res.data);
                        this.msg = res.data[0].title
                    }).catch((err) => {
                        
                    });
                }
            }
        })
    </script>
</body>
</html>
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值