Axios简单使用与配置安装-Vue

安装Axios

npm i axios

main.js 导入

javascript 复制代码
import Axios from 'axios'
Vue.prototype.$axios = Axios

简单发送请求

get

javascript 复制代码
getTest() {
        this.$axios({
          method: 'GET',
          url: 'https://apis.jxcxin.cn/api/title?url=https://apis.jxcxin.cn/'
        }).then(res => {
        //请求成功回调
          console.log(res)
        })
      }

post

javascript 复制代码
postTest() {

 //  let data = new URLSearchParams() 表单方式
        this.$axios({
          method: 'POST',
          url: 'https://service3.91suke.com/sk/login',
          //data:data 表单方式
          data:
          {
            id: 120,
            name: '123'
            //post请求参数  这种写法是json格式的
            //		 {id:120,name:'123' }
          }
        }).then(res => {//请求成功回
          console.log(res)
        })
      }

put

javascript 复制代码
 putTest() {
        this.$axios({
          method: 'PUT',
          url: 'www.xxxxx.com/alter',
          data:
          {
            id: 123
          }
        }).then(res => {

          consoloe.log(res)
        })
      }

delete

javascript 复制代码
deleteTest() {
        this.$axios({
          method: 'DELETE',
          url: 'www.xxxxx.com/alter?id=100',
        }).then(res => {
          consoloe.log(res)
        })
      }

方法发送请求(推荐)

get

axios.get(url,params)

params,confing 非必须,参数可以直接在url拼接

javascript 复制代码
        this.$axios.get('https://apis.jxcxin.cn/api/title?url=https://apis.jxcxin.cn/',
          {
            params: { //请求参数对象 xxxxx?id=10
              id: 10
            }
          }).then(res => {
           console.log(res)
          })

post

javascript 复制代码
this.$axios.post('https://service3.91suke.com/sk/login',
          {
            id: 123,
            name: '哈哈哈'

          }).then(res => {
            consoloe.log(res)
          })

其他方法一样

相关推荐
pe7er1 小时前
window管理开发环境篇 - 持续更新
前端·后端
We་ct2 小时前
LeetCode 5. 最长回文子串:DP + 中心扩展
前端·javascript·算法·leetcode·typescript
陈随易6 小时前
有生之年系列,Nodejs进程管理pm2 v7.0发布
前端·后端·程序员
冰暮流星6 小时前
javascript之事件代理/事件委托
前端
@yanyu6667 小时前
登录注册功能-明文
vue.js·springboot
陈随易7 小时前
AI时代,你还在坚持手搓文章吗
前端·后端·程序员
里欧跑得慢9 小时前
17. Flutter Hero动画实现:让界面过渡更加优雅
前端·css·flutter·web
IT_陈寒10 小时前
Vue的这个响应式陷阱,我debug了一整天才爬出来
前端·人工智能·后端
cn_mengbei10 小时前
用React Native开发OpenHarmony应用:Reanimated共享元素过渡
javascript·react native·react.js
kyriewen10 小时前
前端测试:别为了100%覆盖率而写测试,那是自欺欺人
前端·javascript·单元测试