vue基础4.2--axios的使用

本文介绍Axios库的基础使用方法,包括获取数据、传参、发布书籍及全局配置等内容。通过具体示例展示如何利用Axios进行HTTP请求操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2. axios

2.0 axios基本使用

axios文档

特点

  • 支持客户端发送Ajax请求
  • 支持服务端Node.js发送请求
  • 支持Promise相关用法
  • 支持请求和响应的拦截器功能
  • 自动转换JSON数据
  • axios 底层还是原生js实现, 内部通过Promise封装的

axios的基本使用

axios({
  method: '请求方式', // get post
  url: '请求地址',
  data: {    // 拼接到请求体的参数,  post请求的参数
    xxx: xxx,
  },
  params: {  // 拼接到请求行的参数, get请求的参数
   	xxx: xxx 
  }
}).then(res => {
  console.log(res.data) // 后台返回的结果
}).catch(err => {
  console.log(err) // 后台报错返回
})

2.1 axios基本使用-获取数据

例子如下:

components/UseAxios.vue

<template>
  <div>
    <p>1. 获取所有图书信息</p>
    <button @click="getAllFn">点击-查看控制台</button>
  </div>
</template>

<script>
// 目标1: 获取所有图书信息
// 1. 下载axios
// 2. 引入axios
// 3. 发起axios请求
import axios from "axios";
export default {
  methods: {
    getAllFn() {
      axios({
        url: "http://123.57.109.30:3006/api/getbooks",
        method: "GET", // 默认就是GET方式请求, 可以省略不写
      }).then((res) => {
        console.log(res);
      });
      // axios()-原地得到Promise对象
    },
  }
};
</script>

2.2 axios基本使用-传参

例子如下:

components/UseAxios.vue

<template>
  <div>
    <p>2. 查询某本书籍信息</p>
    <input type="text" placeholder="请输入要查询 的书名" v-model="bName" />
    <button @click="findFn">查询</button>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      bName: ""
    };
  },
  methods: {
    // ...省略了查询所有的代码
    findFn() {
      axios({
        url: "/api/getbooks",
        method: "GET",
        params: { // 都会axios最终拼接到url?后面
            bookname: this.bName
        }
      }).then(res => {
          console.log(res);
      })
    }
  },
};
</script>

2.3 axios基本使用-发布书籍

例子如下:

components/UseAxios.vue

<template>
  <div>
    <p>3. 新增图书信息</p>
    <div>
        <input type="text" placeholder="书名" v-model="bookObj.bookname">
    </div>
    <div>
        <input type="text" placeholder="作者" v-model="bookObj.author">
    </div>
    <div>
        <input type="text" placeholder="出版社" v-model="bookObj.publisher">
    </div>
    <button @click="sendFn">发布</button>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      bName: "",
      bookObj: { // 参数名提前和后台的参数名对上-发送请求就不用再次对接了
          bookname: "",
          author: "",
          publisher: ""
      }
    };
  },
  methods: {
    // ...省略了其他代码
    sendFn(){
       axios({
           url: "/api/addbook",
           method: "POST",
           data: {
               appkey: "7250d3eb-18e1-41bc-8bb2-11483665535a",
               ...this.bookObj
            // 等同于下面
            // bookname: this.bookObj.bookname,
            // author: this.bookObj.author,
            // publisher: this.bookObj.publisher
           }
       }) 
    }
  },
};
</script>

2.4 axios基本使用-全局配置

axios.defaults.baseURL = "http://123.57.109.30:3006"

// 所有请求的url前置可以去掉, 请求时, axios会自动拼接baseURL的地址在前面
getAllFn() {
    axios({
        url: "/api/getbooks",
        method: "GET", // 默认就是GET方式请求, 可以省略不写
    }).then((res) => {
        console.log(res);
    });
    // axios()-原地得到Promise对象
},
{ "name": "ims-web-view", "version": "0.1.0", "private": true, "scripts": { "serve": "vue-cli-service serve", "build": "vue-cli-service build", "test:unit": "vue-cli-service test:unit", "analyze": "cross-env ANALYZE=true vue-cli-service build", "build:dameng": "vue-cli-service build --mode dameng", "build:mofan": "vue-cli-service build --mode mofan", "build:prod": "vue-cli-service build", "build:stage": "vue-cli-service build --mode staging", "build:sxf": "vue-cli-service build --mode sxf", "build:uat": "vue-cli-service build --mode uat" }, "dependencies": { "@babel/runtime": "^7.25.7", "@fullcalendar/core": "^6.1.15", "@fullcalendar/daygrid": "^6.1.15", "@fullcalendar/interaction": "^6.1.15", "@fullcalendar/resource-timeline": "^6.1.15", "@fullcalendar/timegrid": "^6.1.15", "@fullcalendar/timeline": "^6.1.15", "@fullcalendar/vue": "^6.1.15", "@jiaminghi/data-view": "^2.10.0", "@riophae/vue-treeselect": "^0.4.0", "@sentry/tracing": "^7.118.0", "@sentry/vue": "^7.118.0", "@stomp/stompjs": "^7.0.0", "@tencentcloud/call-uikit-vue": "^4.0.7", "@tencentcloud/call-uikit-vue2": "^4.0.7", "@tencentcloud/chat-uikit-vue": "^2.5.1", "@vant/area-data": "^1.5.1", "alloyfinger": "^0.1.16", "awe-dnd": "^0.3.4", "axios": "^1.7.7", "core-js": "^3.38.1", "dayjs": "^1.11.13", "dhtmlx-gantt": "^8.0.10", "dom-to-image-hm": "^2.6.1", "dompurify": "^3.2.5", "echarts-wordcloud": "^2.1.0", "el-table-infinite-scroll": "^2.0.2", "element-resize-detector": "^1.2.4", "element-ui": "^2.15.14", "file-saver": "^2.0.5", "html2canvas": "^1.4.1", "jquery": "^3.7.1", "js-cookie": "^3.0.5", "js-md5": "^0.8.3", "jsencrypt": "^3.3.2", "jsonwebtoken": "^9.0.2", "jspdf": "^1.5.3", "jszip": "^3.10.1", "lodash": "^4.17.21", "moment": "^2.30.1", "node-forge": "^1.3.1", "normalize.css": "^8.0.1", "nprogress": "^0.2.0", "path-to-regexp": "^6.3.0", "posthog-js": "^1.230.4", "qrcodejs2": "0.0.2", "qs": "^6.13.0", "quill-image-extend-module": "^1.1.2", "recorder-core": "^1.2.23020100", "recordrtc": "^5.6.2", "save": "^2.4.0", "screenfull": "^6.0.2", "sm-crypto": "^0.3.13", "sortablejs": "^1.15.3", "svg-sprite-loader": "^6.0.11", "svgo": "^1.3.2", "swiper": "^5.4.5", "tim-upload-plugin": "^1.4.2", "trtc-sdk-v5": "^5.1.3", "turndown": "^7.2.0", "v-viewer": "^1.6.4", "vant": "^2.12.54", "vue": "^2.7.16", "vue-amap": "^0.5.10", "vue-awesome-swiper": "^4.1.1", "vue-bus": "^1.2.1", "vue-carousel": "^0.18.0", "vue-class-component": "^7.2.3", "vue-clipboard2": "^0.3.3", "vue-clock2": "^1.1.5", "vue-count-to": "^1.0.13", "vue-gallery": "^2.0.1", "vue-i18n": "^8.28.1", "vue-js-toggle-button": "^1.3.3", "vue-markdown": "^2.2.4", "vue-orgchart": "^1.1.7", "vue-property-decorator": "^9.1.2", "vue-qr": "^2.3.0", "vue-quill-editor": "^3.0.6", "vue-router": "^3.6.5", "vue-splitpane": "^1.0.6", "vue-wordcloud": "^1.1.1", "vue2-org-tree": "^1.3.6", "vuex": "^3.6.2", "vxe-table": "^3.11.14", "vxe-table-plugin-export-xlsx": "^2.2.1", "vxe-utils": "^1.9.3", "wangeditor": "^4.7.15", "xe-ajax": "^4.0.3", "xe-utils": "^3.4.0", "xlsx": "^0.18.5" }, "devDependencies": { "@babel/plugin-transform-runtime": "^7.25.7", "@riophae/vue-treeselect": "^0.4.0", "@types/chai": "^4.2.15", "@types/mocha": "^8.2.1", "@types/node": "^16.18.108", "@vue/cli-plugin-babel": "^5.0.8", "@vue/cli-plugin-router": "^5.0.8", "@vue/cli-plugin-typescript": "~5.0.0", "@vue/cli-plugin-unit-mocha": "^5.0.8", "@vue/cli-plugin-vuex": "^5.0.8", "@vue/cli-service": "^5.0.8", "@vue/preload-webpack-plugin": "^2.0.0", "@vue/test-utils": "^1.3.6", "awe-dnd": "^0.3.4", "babel-plugin-component": "^1.1.1", "babel-plugin-import": "^1.13.8", "buffer": "^6.0.3", "chai": "^4.5.0", "compression-webpack-plugin": "^11.1.0", "cross-env": "^7.0.3", "crypto-browserify": "^3.12.1", "css-loader": "^7.1.2", "cssnano": "^7.0.6", "dom-to-image-hm": "^2.6.1", "echarts": "^5.5.1", "file-loader": "^6.2.0", "https-browserify": "^1.0.0", "less": "^4.2.0", "less-loader": "^12.2.0", "lodash": "^4.17.21", "mini-css-extract-plugin": "^2.9.1", "os-browserify": "^0.3.0", "path-browserify": "^1.0.1", "process": "^0.11.10", "sass": "^1.79.4", "sass-loader": "^16.0.2", "sockjs-client": "^1.6.1", "stream-browserify": "^3.0.0", "stream-http": "^3.2.0", "string-replace-loader": "^3.2.0", "terser-webpack-plugin": "^5.3.10", "timers-browserify": "^2.0.12", "typescript": "~4.5.5", "url": "^0.11.4", "url-loader": "^4.1.1", "util": "^0.12.5", "vm-browserify": "^1.1.2", "vue-amap": "^0.5.10", "vue-style-loader": "^4.1.3", "vue-template-compiler": "^2.7.16", "webpack-bundle-analyzer": "^4.10.2" }, "browserslist": [ "> 1%", "last 2 versions", "not dead" ] } 那个是我的node版本这是package.json 文件
最新发布
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咖啡壶子

你的鼓励奖是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值