vue3+vite+js axios引用

devtools/2024/10/21 10:04:26/

先交代下基础版本:
“node”:“V16.14.1” “vue”: “^3.4.21” “vite”: “^5.2.0”

  1. 安装:npm install axios --save
  2. 在src目录下的utils文件夹创建一个request.js文件(示例代码,仅供参考):
//引入axios请求
import axios from 'axios'
// store
import useUserStore from '@/store/modules/userStore.js'
const store = useUserStore()
const BASE_API=import.meta.env.VITE_APP_BASE_API
// 创建axios实例
const service = axios.create({baseURL: BASE_API, //所有的后端接口请求地址前缀部分(没有后端请求不用写)timeout: 15000 // 请求超时时间,这里15秒//withCredentials: true,// 异步请求携带cookie,true为携带,false为不携带//请求头里面设置通用传参类型/*headers: {//设置后端需要的传参类型'Content-Type': 'application/json','token': 'x-auth-token',//一开始就要token'X-Requested-With': 'XMLHttpRequest',}*/
})// request拦截器
service.interceptors.request.use(config => {const value = JSON.parse(localStorage.getItem('token'));console.log(111, value.token)if (store.token) {config.headers['token'] = store.token // 让每个请求携带自定义token 请根据实际情况自行修改}return config
}, error => {console.log(error)return Promise.reject(error)
})// response拦截器
service.interceptors.response.use(response => {//对数据返回做什么const res = response.dataconst config = response.config// custom 表示自定义if (res.status_code !== 200 && !config.custom) {//   if (res.status_code === 1002) {//     loginOut()//   } else if (loginVerify(res, config)) { // 登录功能验证//     return res//   } else {// Message({//   message: res.status || 'Error',//   type: 'error',//   duration: 5 * 1000// })//   }return Promise.reject(new Error(res.status || 'Error'))} else {if (response.headers['authorization'] || response.headers['Authorization']) {const _token = response.headers['authorization'] || response.headers['Authorization']store.dispatch('user/setToken', _token.split('Bearer ')[1])}return res}},error => {console.log('err' + error) // for debug// Message({//   message: error.message,//   type: 'error',//   duration: 5 * 1000// })return Promise.reject(error)}
)
export default service
  1. src目录下创建一个api斜体样式文件夹,用来存放每个模块的接口请求,类如login.js
import request from '@/utils/request'//示例以application/json格式传参
export function login(data) {return request({url: '/admin/login',method: 'post',data: data})
}//示例在url后面拼接参数传参
export function test(params) {return request({url: '/admin/login',method: 'post',params: params})
}
  1. 使用,类如HelloWorld.vue:
<script setup>
import { login } from '@/api/login.js'
onMounted(() => {login({ phone: 18888888888 }).then(res => {console.log(res)}).catch(res => {console.log(res)})
})
</script>

http://www.ppmy.cn/devtools/32432.html

相关文章

【综述】多核处理器芯片

文章目录 前言 Infineon处理器 AURIX™系列 TC399XX-256F300S 典型应用 开发工具 参考资料 前言 见《【综述】DSP处理器芯片》 Infineon处理器 AURIX™系列&#xff0c;基于TriCore内核&#xff0c;用于汽车和工业领域。 XMC™系列&#xff0c;基于ARM Cortex-M内核&…

NodeJs入门知识

**************************************************************************************************************************************************************************** 1、配置Node.js与npm下载&#xff08;精力所致&#xff0c;必有精品&#xff09; …

c++ Lambda表达式 简单实验

C11引入的Lambda表达式是一种非常强大的特性&#xff0c;它允许我们定义匿名函数对象。Lambda表达式常用于回调函数、STL算法中需要传递函数对象的场景等。下面是一个简单的C Lambda表达式的实验&#xff1a; #include <iostream> #include <vector> #include…

模型训练常见超参数的讲解

一、latent_dim(潜在空间的维度) 在模型训练中,潜在空间(latent space)是指嵌入在模型内部的一种低维、通常连续的表示空间,尤其是在无监督学习或生成模型(如自编码器、变分自编码器VAEs、生成对抗网络GANs)中。潜在空间的维度(latent dimensionality)是指这个空间的…

微服务之SpringCloud AlibabaSeata处理分布式事务

一、概述 1.1背景 一次业务操作需要跨多个数据源或需要跨多个系统进行远程调用&#xff0c;就会产生分布式事务问题 but 关系型数据库提供的能力是基于单机事务的&#xff0c;一旦遇到分布式事务场景&#xff0c;就需要通过更多其他技术手段来解决问题。 全局事务&#xff1a;…

OC类与对象上半章节较为重要的部分

OC类与对象上半章节较为重要的部分 文章目录 OC类与对象上半章节较为重要的部分前言OC对象的本质OC的实例对象&#xff0c;类对象&#xff0c;元类对象isa和superclass分析分析isKindOfClass和isMemberOfClass方法->和点语法的区别 前言 分享会的时候对于isKindOfClass和isM…

根据前序遍历求树的各种遍历

//创建二叉树和三种遍历,输入序列如 1 5 8 0 0 0 6 0 0 #include <stdio.h> #include <iostream> using namespace std;struct Node{int data;Node* left;Node* right; };Node* create(void){int x;cin>>x;if(x0){return NULL;}Node *T new Node;T->dat…

Linux系统的source命令详解

目录 一、命令介绍 二、基本用法 三、使用场景 1、环境变量 2、函数和别名 3、配置文件 三、命令示例 1、一般的脚本文件 2、使用source的效果 四、使用 source 命令的重要性 1、修改当前 shell 会话的环境 2、加载配置文件 3、在当前 shell 会话中测试脚本 五、…