vue3+vite+js配置路径别名

发布时间 2023-06-28 14:29:02作者: 紫花地丁year

1、让vscode认识@符号

  项目下新建jsconfig.json,配置 baseUrl,paths 参数

{
  "compilerOptions": {
    "target": "esnext",
    "useDefineForClassFields": true,
    "module": "esnext",
    "moduleResolution": "node",
    "strict": true,
    "jsx": "preserve",
    "sourceMap": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "esModuleInterop": true,
    "lib": ["esnext", "dom"],
    "skipLibCheck": true,
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    }
  },
  "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
  "references": [{ "path": "./tsconfig.node.json" }]
}

2、二、让vite识别@符号

  安装node的类型声明文件

 npm i  @types/node -D

在vite.config.ts中增加配置

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// 这里使用nodejs的模块,可能会报错,需要安装node的类型声明文件
const path = require('path')
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    '*': path.resolve('') } } })

 

参考文章

https://blog.csdn.net/jhfvuyhgui/article/details/125750854

https://blog.csdn.net/qq_45874251/article/details/125288255