react18 配置路径别名

vite.config.js

import {
    
     defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import * as path from 'path'
// https://vitejs.dev/config/
export default defineConfig({
    
    
  plugins: [react()],
  //跨域配置
  server: {
    
    
    port: 8080,
    proxy: {
    
    
      // 接口地址代理
      '/api': {
    
    
        target: 'http://127.0.0.1:8010', // 接口的域名
        secure: false, // 如果是https接口,需要配置这个参数
        changeOrigin: true, // 如果接口跨域,需要进行这个参数配置
        rewrite: path => path.replace(/^\/api/, '')

      },
    },
    host: '0.0.0.0'
  },
  //路径别名信息
  resolve: {
    
    
    alias: {
    
    
      '@': path.resolve(__dirname, './src')
    }
  }
})

tsconfig.json文件

{
    
    
    "compilerOptions": {
    
    
      "target": "es5",
      "lib": [
        "dom",
        "dom.iterable",
        "esnext"
      ],
      "allowJs": true,
      "skipLibCheck": true,
      "esModuleInterop": true,
      "allowSyntheticDefaultImports": true,
      "strict": true,
      "forceConsistentCasingInFileNames": true,
      "noFallthroughCasesInSwitch": true,
      "module": "esnext",
      "moduleResolution": "node",
      "resolveJsonModule": true,
      "isolatedModules": true,
      "noEmit": true,
      "jsx": "react-jsx",
      //路径别名需要配置  baseUrl和paths
      "baseUrl": "./",
      "paths": {
    
    
        "@/*":[
            "src/*"
        ]
      }
    },
    "include": [
      "src"
    ]
  }
  

之后便可以使用

猜你喜欢

转载自blog.csdn.net/weixin_47287832/article/details/128347664