关于vite:vite-中使用-配置路径别名

55次阅读

共计 778 个字符,预计需要花费 2 分钟才能阅读完成。

1. 关上 vite.config.ts 文件,配置 alias 参数

import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
const path = require('path');


// https://vitejs.dev/config/
export default defineConfig({plugins: [vue()],
  resolve:{
    // 设置门路别名
    alias: {'@': path.resolve(__dirname, './src'),
      '*': path.resolve('')
      },
  }
  
})

2. 关上 tsconfig.json 文件

{
  "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"}]
}

3. 从新运行

4. 应用

import Home from '@/views/index/index.vue'

正文完
 0