1. query方式:[https://www.yuque.com/](https://www.yuque.com/)?id=666&title=你好"(axios中params参数传递格式就是这样)
  2. params方式:https://www.yuque.com/666/你好 (axios中通过在链接尾部添加 +’666’+’你好’’)

    基础使用 - axios()

    1. const btn = document.querySelectorAll('button');
    2. //获取
    3. btn[0].onclick=()=>{
    4. axios({
    5. method:'get',
    6. url:'http://localhost:3000/posts/1'
    7. }).then(response=>{
    8. console.log(response);
    9. })
    10. }
    11. // 新增
    12. btn[1].onclick=()=>{
    13. axios({
    14. method:'POST',
    15. url:'http://localhost:3000/posts',
    16. data:{
    17. title:'这是更新后的数据',
    18. author:'Dan'
    19. }
    20. }).then(response=>{
    21. console.log(response);
    22. })
    23. }
    24. // 修改
    25. btn[2].onclick=()=>{
    26. axios({
    27. method:'PUT',
    28. url:'http://localhost:3000/posts/2',
    29. data:{
    30. title:'这才是更新后的数据',
    31. author:'是Dan啊'
    32. }
    33. }).then(response=>{
    34. console.log(response);
    35. })
    36. }
    37. // 删除
    38. btn[3].onclick=()=>{
    39. axios({
    40. method:'DELETE',
    41. url:'http://localhost:3000/posts/3'
    42. }).then(response=>{
    43. console.log(response);
    44. })
    45. }

    其他方法 - axios.get()/post/request…

    1. const btn = document.querySelectorAll('button');
    2. btn[0].onclick=()=>{
    3. axios.request({
    4. method:'GET',
    5. url:'http://localhost:3000/posts'
    6. }).then(response=>{
    7. console.log(response);
    8. })
    9. }
    10. btn[1].onclick=()=>{
    11. axios.get('http://localhost:3000/posts').then(response=>{
    12. console.log(response);
    13. })
    14. }
    15. btn[2].onclick=()=>{
    16. axios.post('http://localhost:3000/posts',{
    17. title:'被我用axios.post修改了'
    18. })
    19. }

    传参

    ```javascript // 方式一 data axios({ method: ‘get’, url: ‘/getBranchData’, data: { brahcnID: 1 } });

// 方式二 /getBranchData?brahcnID=1 //payload - brahcnID: 1 axios({ method: ‘get’, url: ‘/getBranchData’, params: { brahcnID: 1 } });

//方式三 /getBranchData/1 axios({ method: ‘get’, url: /getBranchData/${brahcnID} });

//get方法第二个参数为config axios.get(‘/…’,{ params:{}, data:{} })

//post一般只传data axios.post(‘/…’,data)

//post方法第二个为data,第三个为config // /…?xx=xx&xx=xx axios.post(‘/…’,data,{data:{},params:{}})

  1. <a name="KQVSr"></a>
  2. # 默认配置
  3. ```javascript
  4. //默认配置
  5. axios.defaults.method='GET',
  6. axios.defaults.baseURL='http://localhost:3000';
  7. axios.defaults.timeout=3000;
  8. axios({
  9. url:'/posts'
  10. });

axios实例对象

//当要对多个服务器发送请求时可以使用axios.create创建多个对象
//创建实例对象与axios几乎一样
const dan1 = axios.create({
  // baseURL:'http://localhost:3000',
  baseURL:'http://a.com',
  timeout:3000
});
dan1({url:'/posts'}).then(Response=>{console.log(Response)});

//创建另一个实例对象
const dan2 = axios.create({
  // baseURL:'http://localhost:3000',
  baseURL:'http://b.com',
  timeout:3000
});
dan2.get('/posts').then(Response=>{console.log(Response)});

拦截器

let count = 0 //记录请求次数

//发送请求拦截
axios.interceptors.request.use(function(config){
    //'请求拦截成功'
    count++;
    Vue.showLoading();//显示loading
    config.data={a:99}//修改参数

    return config;
},function(error){
    //'请求拦截失败'
    Vue.hiddenLoading()//隐藏loading
    return Promise.reject(error)
});

//响应数据拦截
axios.interceptors.response.use(function(response){
    //'响应拦截成功'
    count--;
    if (count === 0) {
        Vue.hiddenLoading();//隐藏loading
    }
    return response
},function(error){
    //'响应拦截失败'
    // 对响应错误做点什么
    Vue.hiddenLoading();//隐藏loading
    return Promise.reject(error)
});


axios({
    method:'get',
    url:'http://localhost:3000/posts'
}).then(response=>{
    console.log(response);
},error=>{
    console.log(error);
})

取消请求

let cancel = null;
const btn = document.querySelectorAll('button');
//获取
btn[0].onclick=()=>{
  //如果存在任务则取消上一次,防止用户频繁点击按钮
  if(cancel != null){
    cancel()
  };

  axios({
    method:'get',
    url:'http://localhost:3000/posts/1',
    cancelToken:new axios.CancelToken(function(c){
      cancel=c;
    })
  }).then(response=>{
    cancel = null;//初始化cancel
  })
}

插件-qs

https://github.com/ljharb/qs