vite 别名配置注意事项

4/11/2022 vitealias

# vite 别名配置需要2点

  1. Vite config

image-20220411165325332

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import * as path from 'path'
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    react()
  ],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },
  server: {
    port: 3000,
    proxy: {
      '/api': {
        target: 'http://xuanyuan.jinuo.fun:8080/',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  },
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  1. Tsconfig.json

image-20220411165514379

告诉TS解析如何解析某些路径

注意./src/* 后边这个*一定要加

Last Updated: 4/11/2022, 4:58:35 PM