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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章