Vite配置@别名 + Ts
- 在vite.config.ts中添加
import path from 'path'
export default defineConfig({
resolve: {
alias: {
'@': path.resolve(__dirname, "src")
}
}
})
- ts报错找不到模块解决方法,需要安装 Node.js 的声明文件
npm i @types/node -D
- 模块 ""path"" 只能在使用 "allowSyntheticDefaultImports" 标志时进行默认导入 解决方法需要在
tsconfig.node.json
文件中添加
"compilerOptions": {
"allowSyntheticDefaultImports": true
}
- 发现使用@引入报错找不到 Cannot find module '@/xxx' or its corresponding type declarations. 在
tsconfig.json
中添加
"baseUrl": "./",
"paths":{
"@": ["src"],
"@/*": ["src/*"],
}