vite.config.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { defineConfig } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import { resolve } from 'path'; // 主要用于alias文件路径别名
  4. function pathResolve(dir: string) {
  5. return resolve(__dirname, '.', dir);
  6. }
  7. // module.exports = {
  8. // devServer: {
  9. // proxy: {
  10. // '/api': {
  11. // target: 'http://127.0.0.1:8080/api', //接口域名
  12. // changeOrigin: true, //是否跨域
  13. // ws: true, //是否代理 websockets
  14. // secure: false, //是否https接口
  15. // pathRewrite: { //路径重置
  16. // '^/api': ''
  17. // }
  18. // }
  19. // }
  20. // }
  21. // };
  22. export default defineConfig({
  23. plugins: [vue()], // 配置需要使用的插件列表
  24. resolve: {
  25. alias: {
  26. "/@/": pathResolve("src")+'/', // 这里是将src目录配置别名为 /@ 方便在项目中导入src目录下的文件
  27. }
  28. },
  29. server: { //主要是加上这段代码
  30. host: '127.0.0.1',
  31. port: 3000,
  32. proxy: {
  33. '/api': {
  34. target: 'http://127.0.0.1:8080', //实际请求地址
  35. changeOrigin: true,
  36. rewrite: (path) => path.replace(/^\/api/, '')
  37. },
  38. }
  39. }
  40. })