vite+ts 项目如何使用别名@ ?

218 阅读1分钟

配置 vite.config.ts

// https://vitejs.dev/config/
export default defineConfig({
  resolve: {
    alias: {
      '@': resolve(__dirname, './src')
    }
  }
})

使用文件系统路径的别名时,请始终使用绝对路径。相对路径的值会原封不动地使用,因此无法被正常解析。

配置 tsconfig.json

{
  "compilerOptions": {
    "paths": {
      "@/*": [
        "./src/*"
      ]
    }
  }
}

可以正常使用

import { createApp } from 'vue'
import App from "@/App.vue"
createApp(App).mount('#app')