manage项目引入axios,并完成axios基础配置
1、npm install axios 引入axios
2、src下新建http.ts,可根据项目实际情况配置
import axios from 'axios';
const http = axios.create({
baseURL: 'http://localhost:8080', // 基础URL 可以根据你的情况自行修改
timeout: 10000, // 请求超时时间
// 其他配置...
});
// 请求拦截器
http.interceptors.request.use(
config => {
config.headers['Authorization'] = 'Bearer your-token';
config.headers['Content-Type'] = 'application/json';
return config;
},
error => {
// 请求错误处理
return Promise.reject(error);
}
);
// 响应拦截器
http.interceptors.response.use(
response => {
// 对响应数据做处理,例如只返回data部分
return response.data;
},
error => {
// 响应错误处理
return Promise.reject(error);
}
);
export default http;
3、axios请求挂载到vue原生属性
main.ts
import http from './http.ts'
// 挂载axios到Vue原型,名为$http
createApp(App).config.globalProperties.$http = http;
到这里项目就可以直接引用this.$http.get()和this.$http.post()发送接口请求了!

浙公网安备 33010602011771号