vite-vue-ts axios请求

1,安装axios

yarn add axios -S

【vite-vue-ts axios请求】2,新建api文件夹
axios.ts
import axios from 'axios'; let instance = axios.create({ baseURL: 'http://localhost:3555/api' })export default instance;

index.ts
import instance from "./axios"; export const getUrl = () => { return instance({ url: "/users/test" // 请求地址 }) }export default { getUrl };

3,使用
import { getUrl } from "../api/index"export default { setup() { const getUrls = async() =>{ const res = await getUrl() console.log(res,2) } onMounted(() => {//生命周期 getUrls() }) } }

    推荐阅读