31.vue3项目设置自动引入

6/26/2023 笔记

vue3+vite项目设置自动引入Vue API、组件、Element Plus。避免在每个页面重复引入,以此来节省重复代码和提高开发效率。

插件 概念 自动导入对象
unplugin-auto-import 按需自动导入API ref,reactive,watch,computed 等API
unplugin-vue-components 按需自动导入组件 Element Plus 等三方库和指定目录下的自定义组件

# 安装插件依赖

pnpm add unplugin-auto-import unplugin-vue-components -D
1

# 相关配置

  1. 自动导入Vue API
// vite.config.ts
import AutoImport from 'unplugin-auto-import/vite'

export default defineConfig(({ mode }) => {
  return {
    plugins: [
      AutoImport({
        include: [/\.[tj]sx?$/, /\.vue\??/],
        // 自动导入Vue相关函数,如:ref、reactive,toRef等
        // import: ['vue', 'vue-router'],
        imports: [
          'vue',
          'vue-router',
          '@vueuse/core',
          {
            vue: ['createVNode', 'render'],
            'vue-router': [
              'createRouter',
              'createWebHistory',
              'useRouter',
              'useRoute'
            ],
            // 全局使用 _.xxx()
            'lodash-es': [
              // default imports
              ['*', '_'] // import { * as _ } from 'lodash-es',
            ]
          },
          // type import
          {
            from: 'vue',
            imports: [
              'App',
              'ComponentPublicInstance',
              'ComponentPublicInstanceCostom'
            ],
            type: true
          },
          {
            from: 'vue-router',
            imports: [
              'RouteRecordRaw',
              'RouteLocationRaw',
              'LocationQuery',
              'NavigationFailure',
              'RouteParams',
              'RouteLocationNormalizedLoaded',
              'RouteRecordName',
              'NavigationGuard'
            ],
            type: true
          }
        ],
        // 自动导入 Element Plus 相关函数,如:ElMessage, ElMessageBox... (带样式)
        eslintrc: {
          enabled: true, // 是否自动生成 eslint 规则,建议生成之后设置 false 
          // filepath: "./.eslintrc-auto-import.json", //自动导入函数 eslint 规则的文件默认路径: ./.eslintrc-auto-import.json
        },
      });
      // 自动导入组件TS类型声明文件,默认 ./components.d.ts
      Components(),
    ]
  }
})

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  1. .eslintrc.js
"extends": [
  "./.eslintrc-auto-import.json"
],
1
2
3
  1. 自动导入TS类型声明文件引入
// tsconfig.json
{
  "include": ["src/**/*.d.ts"]
}
1
2
3
4
  1. 整合Element Plus
  • 安装Element Plus
pnpm add Element Plus
1
  • 配置
// vite.config.ts
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
export default ({ mode }) => {
  return {
    plugins: [
      // ...
      AutoImport({
        // ...  
        resolvers: [
          // 自动导入 Element Plus 相关函数,如:ElMessage, ElMessageBox... (带样式)
          ElementPlusResolver(),
        ]
        vueTemplate: true, // 是否在 vue 模板中自动导入
        // 自动导入函数TS类型声明文件,默认路径
        dts: './auto-imports.d.ts',
      }),
      Components({ 
        resolvers: [
          // 自动导入 Element Plus 组件
          ElementPlusResolver()
        ],
      }),
    ],
  };
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
最近更新时间: 6/26/2023, 7:08:05 AM
강남역 4번 출구
Plastic / Fallin` Dild