zoukankan      html  css  js  c++  java
  • axios的使用

      axios不光在vue上用,在angular和react上也能用。在vue-resource停止维护后,axios是一个很好的替代品。

      npm install axios -g     安装axios

      import Axios from 'axios';  在main.js中引入axios

      Vue.prototype.$axios = Axios;  将其挂到vue原形链上。

      Axios.defaults.baseURL='http://****';  设置基本请求网址

          created(){
            function test(a,b){
              console.log(a);
              console.log(b);
            }
            //console.log('lal',this.$axios);
            //get请求
              this.$axios.get("lunbo.php",{params:{id:'120'}}).then(res=>{console.log(res)}).catch(err=>{console.log(err)});
              //post请求
            this.$axios.post("msg.php",'content={"name":"lisi","age":"19"}')
            .then(res=>{console.log(res)})
            .catch(err=>{console.log(err)});
    
            //合并请求
            this.$axios.all([
                 this.$axios.get('lunbo.php',{params:{id:20}}),
                 this.$axios.post('msg.php','content={"name":"si","age":"18"}')
              ]).then(this.$axios.spread(test)).catch(err=>{console.log(err)})
          }
    //合并请求,注意接收的函数参数就按顺序接的,test函数的第一个参数是接的get返回的,第二个是post的。

      拦截器

      axios的拦截器就是在请求发送前做些处理,然后发送处理过后的请求。

      Axios.interceptors.request.use(function(config){

             //做一些处理

             return config;  //然后返回

        });

       //得到响应后,在做一些处理,然后返回处理后的结果。

      Axios.interceptors.response.use(function(config){

             //做一些处理

             return config;  //然后返回

        });

  • 相关阅读:
    iframe跨域
    jquery 中的 $(“#”) 与 js中的document.getElementById(“”) 的区别
    jQuery中this与$(this)的区别
    div层调整zindex属性无效原因分析及解决方法
    The local variable......been initialized
    equals方法的重写
    eclipse快捷键
    JAVA中几个常用的方法
    Java基础4(方法基础和一维数组)
    Java基础3笔记
  • 原文地址:https://www.cnblogs.com/sujianfeng/p/8863556.html
Copyright © 2011-2022 走看看