1. import { defineConfig } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import eslintPlugin from 'vite-plugin-eslint'
  4. // Element Plus
  5. import AutoImport from 'unplugin-auto-import/vite'
  6. import Components from 'unplugin-vue-components/vite'
  7. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  8. import ElementPlus from 'unplugin-element-plus/vite'
  9. const path = require('path')
  10. // https://vitejs.dev/config/
  11. export default defineConfig({
  12. plugins: [
  13. vue(),
  14. eslintPlugin({
  15. include: ['src/**/*.js', 'src/**/*.vue', 'src/*.js', 'src/*.vue', 'src/*.ts', 'src/**/*.ts']
  16. }),
  17. AutoImport({
  18. resolvers: [ElementPlusResolver()]
  19. }),
  20. Components({
  21. resolvers: [ElementPlusResolver()]
  22. }),
  23. ElementPlus({
  24. // options
  25. })
  26. ],
  27. resolve: { // 配置别名
  28. alias: {
  29. '@': path.resolve(__dirname, './src/')
  30. }
  31. }
  32. })

上面配置别名后,其实已经可以使用了,但ts依然会报错,解决方案:
第一步:http://www.qianduanheidong.com/blog/article/316839/2027f86491db4c5f9bd5/
第二步:修改tsconfig.json文件

  1. {
  2. "compilerOptions": {
  3. "target": "esnext",
  4. "useDefineForClassFields": true,
  5. "module": "esnext",
  6. "moduleResolution": "node",
  7. "strict": true,
  8. "jsx": "preserve",
  9. "sourceMap": true,
  10. "resolveJsonModule": true,
  11. "isolatedModules": true,
  12. "esModuleInterop": true,
  13. // 指定下面两个选项
  14. "baseUrl": ".",
  15. "paths": {
  16. "@/*": [
  17. "src/*"
  18. ],
  19. },
  20. "lib": ["esnext", "dom"],
  21. "skipLibCheck": true,
  22. "types": ["element-plus/global"]
  23. },
  24. "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
  25. "exclude": ["node_modules"],
  26. "references": [{ "path": "./tsconfig.node.json" }]
  27. }

找不到预编译宏:

  1. module.exports = {
  2. env: {
  3. browser: true,
  4. es2021: true,
  5. node: true,
  6. 'vue/setup-compiler-macros': true // 加上这一句,使用预编译宏
  7. },
  8. extends: ['plugin:vue/essential', 'eslint:recommended', 'plugin:prettier/recommended'],
  9. parserOptions: {
  10. ecmaVersion: 2020,
  11. parser: '@typescript-eslint/parser',
  12. sourceType: 'module'
  13. },
  14. plugins: ['vue', '@typescript-eslint'],
  15. rules: {
  16. 'prettier/prettier': ['error', { endOfLine: 'lf' }],
  17. 'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
  18. 'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
  19. 'no-unused-vars': 'off',
  20. 'vue/multi-word-component-names': [
  21. 'error',
  22. {
  23. ignores: ['index']
  24. }
  25. ]
  26. }
  27. }

Vue typeScript: Could not find a declaration file for module ‘‘. ‘‘ implicitly has an ‘any’…

https://www.jianshu.com/p/02c42fc1ee59