diff --git a/src/api/utils/config.ts b/src/api/utils/config.ts index e3d86bede..c41cdd599 100644 --- a/src/api/utils/config.ts +++ b/src/api/utils/config.ts @@ -4,11 +4,13 @@ import { excludeProps } from "./utils"; * 默认配置 */ export const defaultConfig: AxiosRequestConfig = { + baseURL: '/api', + timeout: 10000, //10秒超时 headers: { Accept: "application/json, text/plain, */*", "Content-Type": "application/json", "X-Requested-With": "XMLHttpRequest" - } + }, }; export function genConfig(config?: AxiosRequestConfig): AxiosRequestConfig { diff --git a/src/api/utils/core.ts b/src/api/utils/core.ts index eb3dd08dc..b3f8b4323 100644 --- a/src/api/utils/core.ts +++ b/src/api/utils/core.ts @@ -22,7 +22,7 @@ class EnclosureHttp { private static initConfig: EnclosureHttpRequestConfig = {}; // 保存当前Axios实例对象 - private static axiosInstance: AxiosInstance; + private static axiosInstance: AxiosInstance = Axios.create(genConfig()); // 保存 EnclosureHttp实例 private static EnclosureHttpInstance: EnclosureHttp; @@ -153,6 +153,7 @@ class EnclosureHttp { const instance = EnclosureHttp.axiosInstance; instance.interceptors.response.use( (response: EnclosureHttpResoponse) => { + // 请求每次成功一次就删除当前canceltoken标记 const cancelKey = this.genUniqueKey(response.config); this.deleteCancelTokenByCancelKey(cancelKey); @@ -191,26 +192,11 @@ class EnclosureHttp { ); } - /** - * @description 获取唯一实例 - * @returns EnclosureHttp实例 - */ - static getInstance(): EnclosureHttp { - if (!this.EnclosureHttpInstance) { - this.axiosInstance = Axios.create(this.initConfig); - this.EnclosureHttpInstance = new EnclosureHttp(); - this.EnclosureHttpInstance.httpInterceptorsRequest(); - this.EnclosureHttpInstance.httpInterceptorsResponse(); - } - return this.EnclosureHttpInstance; - } - - // eslint-disable-next-line class-methods-use-this public request( method: RequestMethods, url: string, param?: AxiosRequestConfig, - axiosConfig?: EnclosureHttpRequestConfig + axiosConfig?: EnclosureHttpRequestConfig, ): Promise { const config = transformConfigByMethod(param, { method, @@ -226,8 +212,7 @@ class EnclosureHttp { } return new Promise((resolve, reject) => { - Axios - .request(config) + EnclosureHttp.axiosInstance.request(config) .then((response: EnclosureHttpResoponse) => { resolve(response.data); }) @@ -252,16 +237,7 @@ class EnclosureHttp { ): Promise { return this.request("get", url, params, config); } - - /** - * 注入初始化配置 - * @description 私有化方法 - * @param config axios配置 - * @returns void 0 - */ - static install(config?: EnclosureHttpRequestConfig): void { - this.initConfig = genConfig(config); - } + } export default EnclosureHttp; diff --git a/src/api/utils/types.d.ts b/src/api/utils/types.d.ts index 78bdc9666..2195bb3c5 100644 --- a/src/api/utils/types.d.ts +++ b/src/api/utils/types.d.ts @@ -45,6 +45,4 @@ export default class EnclosureHttp { params?: T, config?: EnclosureHttpRequestConfig ): Promise - static install(config?: EnclosureHttpRequestConfig): void - static getInstance(): EnclosureHttp } \ No newline at end of file diff --git a/src/views/home.vue b/src/views/home.vue index 5f9c34112..cdb091920 100644 --- a/src/views/home.vue +++ b/src/views/home.vue @@ -6,7 +6,7 @@ import { ref, onMounted, nextTick } from "vue" export default { mounted() { - this.$http.request("get", "https://api.github.com/users") + this.$http.request("get", "/getApi") }, setup() { const text = ref("vue-ts") diff --git a/vite.config.ts b/vite.config.ts new file mode 100644 index 000000000..24a893586 --- /dev/null +++ b/vite.config.ts @@ -0,0 +1,26 @@ +module.exports = { + alias: {}, + // 是否自动在浏览器打开 + open: false, + // 是否开启 https + https: false, + // 服务端渲染 + ssr: false, + /** + * Base public path when served in production. + * @default '/' + */ + /** + * Directory relative from `root` where build output will be placed. If the + * directory exists, it will be removed before the build. + * @default 'dist' + */ + // 反向代理 + proxy: { + '/api': { + target: 'http://127.0.0.1:3000', + changeOrigin: true, + rewrite: path => path.replace(/^\/api/, '') + } + } +} \ No newline at end of file