Vue axios请求拦截器和响应拦截器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" src="../vue/dist/vue.min.js"></script>
    <script type="text/javascript" src="../vue-router/dist/vue-router.min.js"></script>
    <script type="text/javascript" src="../vue-axios/node_modules/axios/dist/axios.min.js"></script>
    <style type="text/css">
        .demo1 {
            width: 4px;
            height: 4px;
            border-radius: 2px;
            background: #68b2ce;
            float: left;
            margin: 0 3px;
            animation: demo1 linear 1s infinite;
            -webkit-animation: demo1 linear 1s infinite;
        }
        .demo1:nth-child(1){
            animation-delay:0s;
        }
        .demo1:nth-child(2){
            animation-delay:0.15s;
        }
        .demo1:nth-child(3){
            animation-delay:0.3s;
        }
        .demo1:nth-child(4){
            animation-delay:0.45s;
        }
        .demo1:nth-child(5){
            animation-delay:0.6s;
        }
        @keyframes demo1
        {
            0%,60%,100% {transform: scale(1);}
            30% {transform: scale(2.5);}
        }
        @-webkit-keyframes demo1
        {
            0%,60%,100% {transform: scale(1);}
            30% {transform: scale(2.5);}
        }
    </style>
</head>
<body>
    <div id="app"></div>
</body>
    <script type="text/javascript">
        Vue.use(VueRouter);
        Vue.prototype.$axios = axios;
        var List = {
            data(){
                return {
                    data:'列表页面'
                }
            },
            template:`
                <div>
                    <span>列表页</span>
                </div>
            `
        }
        var Home = {
          data(){
              return{
                  token:'',
                  isShow:false
              }
          },
          template:`
            <div>
                <div id="loading1" v-show="isShow">
                    <div class="demo1"></div>
                    <div class="demo1"></div>
                    <div class="demo1"></div>
                    <div class="demo1"></div>
                    <div class="demo1"></div>
                </div>
                <h1 v-show="!isShow">Token:{{token}}</h1>
                <button type="button" @click="getToken">获取Token</button>
            </div>
          `,
          methods:{
              getToken(){
                  //请求拦截器
                  this.$axios.interceptors.request.use((config)=>{
                      this.isShow = true;
                      return config;
                  },function (err) {
                      return Promise.reject(err);
                  });
                  //响应拦截器
                  this.$axios.interceptors.response.use((response)=>{
                    this.isShow = false;
                    return response;
                  },function(err){
                      return Promise.reject(err);
                  });
                  this.$axios.get('http://localhost:3000/posts')
                  .then(res=>{
                    console.log(res);
                    this.token = res.data[0]["title"];
                  })
                  .catch(err=>{
                    console.log(err);
                  });
              }
          }
        };
        var router = new VueRouter({
            routes:[
                {
                    path:'/',
                    redirect:'home'
                },
                {
                    path:'/home',
                    name:'home',
                    component:Home
                },
                {
                    path:'/list',
                    name:'list',
                    component:List
                }
            ]
        });
        var App = {
            template: `
                <div>
                    <router-link to="/">首页</router-link>
                    <router-link :to="{name:'list'}">列表</router-link>
                    <router-view></router-view>
                </div>
            `
        };

        new Vue({
            el:'#app',
            components:{
                App:App
            },
            router:router,
            template:`<App />`
        });
    </script>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值