使用 Vite 为 React 配置 Alias
Vite 创建的 React 配置 Alias,以便能够使用@作为路径导入模块
安装依赖
Vite 创建的 React 项目,通常情况下没有安装 @types/node,会导致 import path 报错
1 | npm install @types/node --save-dev |
修改 vite.config.ts
添加 resolve 部分
1 | import { defineConfig } from "vite"; |
修改 tsconfig.app.json
在 compilerOptions 中添加 Path mapping 部分
1 | { |
配置完成后,即可使用 @ 作为路径导入依赖
1 | import { MyUtil } from '@/utils/my-util' |