基于Vite+Vue3 给项目引入Axios

基于Vite+Vue3 给项目引入Axios,方便与后端进行通信。

系列文章指路👉

系列文章-基于Vue3创建前端项目并引入、配置常用的库和工具类

安装依赖

npm install axios

新建src/config/config.js 用于存放常用配置

const config = {
   
   baseUrl: '/boot-test'
}

export default config

进行简单封装

引用阶段先简单封装,后续有复杂功能添加进来再进行改造。

src目录下,新建utils,axios两个文件夹,新建index.js
在这里插入图片描述

import axios from 'axios'
import config from "@/config/config.js";

const http = axios.create()
http.defaults.baseURL = config.baseUrl
http.defaults.timeout = 15 * 1000
http.defaults.headers.common['Content-Type'] = 'application/json;charset=UTF-8'

export default http

解决跨域问题

复用我们之前的后端项目,作为Vue3的后端:系列文章-基于SpringBoot3创建项目并配置常用的工具和一些常用的类
需要解决跨域问题,前后端解决都可以,我们本次采用前端解决。
2

    server: {
   
        open: true,// 启动项目自动弹出浏览器
        port: 4000,// 启动端口
        proxy: {
   
            '/boot-test': {
   
                target: 'http://localhost:8001/boot-test',	// 实际请求地址
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/boot-test/, '')
            },
        }
    },

调用尝试

写个小demo,把后端请求到的水果列表展示到前端表格上。
2

<template>
  <h3>
    展示水果列表
  </h3>
  <div style="width: 1000px;">
    <a-table bordered  :scroll="{ y: 300 }"
             :dataSource="tableData" :columns="tableColumn"/>
  </div>

</template>

<script setup>
import {
     ref} from 'vue';
import http from "@/utils/axios/index.js";

let tableData = ref([])
let tableColumn = initColumns()
getAjaxData()

function getAjaxData() {
     
  http.get("/goods/fruit/list")
      .then(resp => {
     
        if (resp && resp.data.code === 200) {
     
          tableData.value = resp.data.data
        }
      })
      .catch(err => {
     
      })
}
function initColumns(){
     
  let columns = []
  columns.push(
      {
     
        title: '编码',
        dataIndex: 'frCode',
        key: 'frCode',
        minWidth: 150
      },
      {
     
        title: '名称',
        dataIndex: 'frName',
        key: 'frName',
        minWidth: 200
      },
      {
     
        title: '价格',
        dataIndex: 'frPrice',
        key: 'frPrice',
        minWidth: 120
      },
  )
  return columns
}
</script>

<style scoped>

</style>

相关推荐

  1. 基于axios请求添加token

    2023-12-23 06:40:05       9 阅读
  2. vue3+vite+js axios引用

    2023-12-23 06:40:05       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-23 06:40:05       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-23 06:40:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-23 06:40:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-23 06:40:05       18 阅读

热门阅读

  1. 【Stress-ng】CentOS 7 离线安装Stress-ng

    2023-12-23 06:40:05       43 阅读
  2. 面试建议篇(持续更新....)

    2023-12-23 06:40:05       40 阅读
  3. Redis缓存设计模式与失效策略

    2023-12-23 06:40:05       40 阅读
  4. php pcntl_fork 多进程

    2023-12-23 06:40:05       32 阅读
  5. 在centos上安装python人脸库face_recognition

    2023-12-23 06:40:05       42 阅读
  6. Hive-DDL详解(超详细)

    2023-12-23 06:40:05       33 阅读
  7. WEB渗透—PHP反序列化(七)

    2023-12-23 06:40:05       35 阅读
  8. KAZE+GTM 图像配准标定 Matlab 实现

    2023-12-23 06:40:05       36 阅读
  9. 达梦的SQL脚本转成Oracle的SQL脚本需要注意什么?

    2023-12-23 06:40:05       40 阅读
  10. 阿里云常用配置:日志采集、OSS、RAM 权限策略

    2023-12-23 06:40:05       42 阅读
  11. Android 13 内置可卸载的搜狗输入法

    2023-12-23 06:40:05       49 阅读