引言
每次打开都要面对满屏的import { ref, reactive } from 'vue' 和 import { Button, Input } from 'antd',导致文件冗长,自动引入插件来帮你!!
一、unplugin-auto-import
npm包地址:https://www.npmjs.com/package/unplugin-auto-import
安装
devDependencies:
npm i -D unplugin-auto-import
1.1 示例
如果依赖一堆文件内容变得冗长,而使用之后代码清晰简洁~
without
import {
computed,
ref,
watch,
watchEffect,
unref,
inject,
provide
} from 'vue'
const count = ref(0)
const doubled = computed(() => count.value * 2)
// ......
with
const count = ref(0)
const doubled = computed(() => count.value * 2)
1.2 配置
在根目录下vite.config.ts文件引入插件:
import AutoImport from 'unplugin-auto-import/vite'
export default defineConfig({
plugins: [
vue(),
vueJsx(),
// 引入!!
AutoImport({
imports: ['vue', 'pinia', 'vue-router'],
dts: 'src/auto-imports.d.ts' // 可填写路径+文件名
})
],
ctrl+s保存之后,会自动在dts填写的路径下生成对应文件,声明文件如下:

/* eslint-disable */
/* prettier-ignore */
// @ts-nocheck
// noinspection JSUnusedGlobalSymbols
// Generated by unplugin-auto-import
// biome-ignore lint: disable
export {
}
declare global {
const EffectScope: typeof import('vue')['EffectScope']
const acceptHMRUpdate: typeof import('pinia')['acceptHMRUpdate']
const computed: typeof import('vue')['computed']
const createApp: typeof import('vue')['createApp']
const createPinia: typeof import('pinia')['createPinia']
const customRef: typeof import('vue')['customRef']
const defineAsyncComponent: typeof import('vue')['defineAsyncComponent']
const defineComponent: typeof import('vue')['defineComponent']
const defineStore: typeof import('pinia')['defineStore']
const effectScope: typeof import('vue')['effectScope']
const getAct
4193



被折叠的 条评论
为什么被折叠?



