1, api.env.ts
export const url = import.meta.env.VITE_APP_API || urlPrefix();
export const urlAll = import.meta.env.VITE_APP_API_ALL || urlPrefix()
function urlPrefix() {
// 获取浏览器路径
const origin = window.location.origin;
// 获取端口号
const port = window.location.port;
// 将路径重新拼接
return `${origin.substring(0, origin.length - port.length)}3014`;
}
2, .env.development
NODE_ENV = development
VITE_API_Mock_ = 1
VITE_APP_API = 'http://xx.xx.xxx.xxx:3014'
VITE_APP_API_ALL = '/api'
3, .env.production
NODE_ENV = production
VITE_API_Mock_ = 0
# VITE_APP_API_ALL =
4, .env.production
import { url } from "@/server/api.env";
import { urlAll } from "@/server/api.env";
export default {
getAsa: {
method: "get",
url: `${url}/corek/wau/auth1/kk`,
},
upload: {
method: "post",
url: `${urlAll}/api/attachment/upload`,
},
}
4,vite.config.ts
server: {
hmr: true,
host: "0.0.0.0",
port: 8080,
proxy: {
"^/api": {
target: "http://xx.xx.xxx.xxx:30014/",
changeOrigin: true, //开启代理
rewrite: (path) => path.replace(/^\/api/, ""),
},
},
},