最近在Vue3项目中使用Vuex和axios-mapper,与之前稍有不同,参考了Github的代码,在这里记录一下。
创建一个api目录,创建一个ts文件来封装处理axios
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
import HttpClient, { HttpClientConfig } from 'axios-mapper'
import networkConfig from '../config/network.config' const https = (hasToken: Boolean = true) => { const config: HttpClientConfig = { baseURL: networkConfig.baseUrl, headers: { "content-type": networkConfig.contentType } } return new HttpClient(config) } export default https
|
创建一个api.ts统一管理业务中的接口
1 2 3 4 5 6 7 8
| import https from './index'; import { ContentType, Method } from 'axios-mapper'
const test= (data?: any) => https().request('/xx/xxx', Method.GET, data, ContentType.json) export { test }
|
具体使用
1 2 3 4 5
| import { test} from "../../api/api"; test().then(res => console.log(res)) or const res = await test()
|