Vue - axios 的使用,及请求接口的几种方式

一. 介绍

Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。
了解更多:https://www.kancloud.cn/yunye/axios/234845

二. 项目中 安装使用 axios

  1. 安装 axios
    npm install axios

  2. main.js 页面引入 axios ,并配置默认请求接口
    在这里插入图片描述

    //配置axios,设置默认请求接口地址
    import axios from 'axios'
    axios.defaults.baseURL="http://xxxx:9810"
    Vue.prototype.$axios=axios
    
  3. 组件中使用

    this.$axios.post('/url接口地址').then((res)=>{
      console.log(res)
    })
    

三. axios 请求接口的五种方式

  • get:一般多用于获取数据
  • post:主要提交表单数据和上传文件
  • put:对数据全部进行更新
  • patch:只对更改过的数据进行更新
  • delete:删除请求

1.get 方式

1.1 不带参数请求接口

  • 第一种方式
    axios.get('url接口地址').then((res)=>{
      console.log(res)
    }),
    
  • 第二种方式
    axios({
      method:'get',
      url:'url接口地址',
    }).then((res)=>{
      console.log(res)
    }),
    

1.2 带参数请求接口(params传参)

  • 第一种方式
    axios.get('url接口地址',{
      params:{
        id:1
      }
    }).then((res)=>{
      console.log(res)
    }),
    
    const obj={
      id:1
    }
    axios.get('url接口地址',{
      params:obj
    }).then((res)=>{
      console.log(res)
    }),
    
  • 第二种方式
    axios({
      method:'get',
      url:'url接口地址',
      params:{
        id:1
      },
    }).then((res)=>{
      console.log(res)
    }),
    
    const obj={
      id:1
    }
    axios({
      method:'get',
      url:'url接口地址',
      params:obj,
    }).then((res)=>{
      console.log(res)
    }),
    

2. post 方式

2.1 不带参数请求接口

  • 第一种方式
    axios.post('url接口地址').then((res)=>{
      console.log(res)
    }),
    
  • 第二种方式
    axios({
      method:'post',
      url:'url接口地址',
    }).then((res)=>{
      console.log(res)
    }),
    

2.2 带参数请求接口

  • 第一种方式

    axios.post('url接口地址',{
        id:1
      }).then((res)=>{
      	console.log(res)
    }),
    
    const obj={
      id:1
    }
    axios.post('url接口地址',obj).then((res)=>{
      	console.log(res)
    }),
    
  • 第二种方式

    axios({
      method:'post',
      url:'url接口地址',
      data:{
        id:1
      },
    }).then((res)=>{
      console.log(res)
    }),
    
    const obj={
      id:1
    }
    axios({
      method:'post',
      url:'url接口地址',
      data:obj,
    }).then((res)=>{
      console.log(res)
    }),
    

3. put 方式

const obj={
  id:1
}
axios.put('url接口地址',obj).then(res=>{
  console.log(res)
})

4. patch 方式

const obj={
  id:1
}
axios.patch('url接口地址',obj).then(res=>{
  console.log(res)
}),

5. delete 方式

当写法参数为 params 时,请求接口时参数会放在URL里面

  • 第一种方式
    axios.delete('url接口地址',{
       params:{
         id:1
       }
     }).then(res=>{
       console.log(res)
     })
    
  • 第二种方式
    axios.delete('url接口地址',{
      data:{
        id:1
      }
    }).then(res=>{
      console.log(res)
    })
    
  • 7
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值