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;  //然后返回

        });

  • 相关阅读:
    下标处理问题
    C++输入输出流
    gcc和gdb
    B2C、C2C电子商务分析
    转载:java 动态代理学习(Proxy,InvocationHandler)
    Java Web开发中路径问题小结
    64位操作系统IIS降为32 位版本运行处理
    SQL Server 2000/2005 数据库分页
    iBatis简单入门教程
    JAVA中的Class类
  • 原文地址:https://www.cnblogs.com/sujianfeng/p/8863556.html
Copyright © 2011-2022 走看看