unplugin-auto-import
是基于 unplugin 写的,支持 vite、webpack、rollup、esbuild
多个打包工具。我们可以使用unplugin-auto-import实现依赖的自动导入,不用再频繁导入依赖包,从而提交我们的开发效率。如下,以vue3+vite
中使用改插件为例。
安装
使用前我们需要先安装该依赖:
$ pnpm i unplugin-auto-import -d
配置
安装好依赖后,我们就可以在vite
中配置改插件了,其在vite中的配置方法如下:
// vite.config.ts import autoimport from 'unplugin-auto-import/vite' export default defineconfig({ plugins: [ autoimport({ imports: [ 'vue', 'vue-router', 'vue-i18n', '@vueuse/core', 'pinia', ], dts: 'types/auto-imports.d.ts', // 使用typescript,需要指定生成对应的d.ts文件或者设置为true,生成默认导入d.ts文件 dirs: ['src/stores', 'src/composables', 'src/hooks'], }), ], })
如上就配置好了一个自动导入的插件,我们通过autoimport插件实现了初始化导入vue、vue-router等依赖库,设置本地项目自动导入路径。其生成的声明文件如下:
/* eslint-disable */ /* prettier-ignore */ // @ts-nocheck // noinspection jsunusedglobalsymbols // generated by unplugin-auto-import export {} declare global { const effectscope: typeof import('vue')['effectscope'] const computed: typeof import('vue')['computed'] const computedasync: typeof import('@vueuse/core')['computedasync'] const computedeager: typeof import('@vueuse/core')['computedeager'] const computedinject: typeof import('@vueuse/core')['computedinject'] const computedwithcontrol: typeof import('@vueuse/core')['computedwithcontrol'] const controlledcomputed: typeof import('@vueuse/core')['controlledcomputed'] const controlledref: typeof import('@vueuse/core')['controlledref'] const createapp: typeof import('vue')['createapp'] const createeventhook: typeof import('@vueuse/core')['createeventhook'] const createglobalstate: typeof import('@vueuse/core')['createglobalstate'] const createinjectionstate: typeof import('@vueuse/core')['createinjectionstate'] const createpinia: typeof import('pinia')['createpinia'] const createreactivefn: typeof import('@vueuse/core')['createreactivefn'] const createreusabletemplate: typeof import('@vueuse/core')['createreusabletemplate'] const createsharedcomposable: typeof import('@vueuse/core')['createsharedcomposable'] const createtemplatepromise: typeof import('@vueuse/core')['createtemplatepromise'] const createunreffn: typeof import('@vueuse/core')['createunreffn'] const customref: typeof import('vue')['customref'] const debouncedref: typeof import('@vueuse/core')['debouncedref'] const debouncedwatch: typeof import('@vueuse/core')['debouncedwatch'] const defineasynccomponent: typeof import('vue')['defineasynccomponent'] const definecomponent: typeof import('vue')['definecomponent'] const definestore: typeof import('pinia')['definestore'] const eagercomputed: typeof import('@vueuse/core')['eagercomputed'] const effectscope: typeof import('vue')['effectscope'] const extendref: typeof import('@vueuse/core')['extendref'] const getactivepinia: typeof import('pinia')['getactivepinia'] const getcurrentinstance: typeof import('vue')['getcurrentinstance'] const getcurrentscope: typeof import('vue')['getcurrentscope'] const h: typeof import('vue')['h'] const ignorablewatch: typeof import('@vueuse/core')['ignorablewatch'] const inject: typeof import('vue')['inject'] const isdark: typeof import('../src/composables/theme')['isdark'] const makedestructurable: typeof import('@vueuse/core')['makedestructurable'] const mapactions: typeof import('pinia')['mapactions'] const mapgetters: typeof import('pinia')['mapgetters'] const mapstate: typeof import('pinia')['mapstate'] const mapstores: typeof import('pinia')['mapstores'] const mapwritablestate: typeof import('pinia')['mapwritablestate'] const markraw: typeof import('vue')['markraw'] const nexttick: typeof import('vue')['nexttick'] const onactivated: typeof import('vue')['onactivated'] const onbeforemount: typeof import('vue')['onbeforemount'] const onbeforerouteleave: typeof import('vue-router')['onbeforerouteleave'] const onbeforerouteupdate: typeof import('vue-router')['onbeforerouteupdate'] const onbeforeunmount: typeof import('vue')['onbeforeunmount'] const onbeforeupdate: typeof import('vue')['onbeforeupdate'] const onclickoutside: typeof import('@vueuse/core')['onclickoutside'] const ondeactivated: typeof import('vue')['ondeactivated'] const onerrorcaptured: typeof import('vue')['onerrorcaptured'] const onkeystroke: typeof import('@vueuse/core')['onkeystroke'] const onlongpress: typeof import('@vueuse/core')['onlongpress'] const onmounted: typeof import('vue')['onmounted'] const onrendertracked: typeof import('vue')['onrendertracked'] const onrendertriggered: typeof import('vue')['onrendertriggered'] const onscopedispose: typeof import('vue')['onscopedispose'] const onserverprefetch: typeof import('vue')['onserverprefetch'] const onstarttyping: typeof import('@vueuse/core')['onstarttyping'] const onunmounted: typeof import('vue')['onunmounted'] const onupdated: typeof import('vue')['onupdated'] const pausablewatch: typeof import('@vueuse/core')['pausablewatch'] const preferredlanguages: typeof import('../src/composables/i18n-locale')['preferredlanguages'] const provide: typeof import('vue')['provide'] const reactify: typeof import('@vueuse/core')['reactify'] const reactifyobject: typeof import('@vueuse/core')['reactifyobject'] const reactive: typeof import('vue')['reactive'] const reactivecomputed: typeof import('@vueuse/core')['reactivecomputed'] const reactiveomit: typeof import('@vueuse/core')['reactiveomit'] const reactivepick: typeof import('@vueuse/core')['reactivepick'] const readonly: typeof import('vue')['readonly'] const ref: typeof import('vue')['ref'] const refautoreset: typeof import('@vueuse/core')['refautoreset'] const refdebounced: typeof import('@vueuse/core')['refdebounced'] const refdefault: typeof import('@vueuse/core')['refdefault'] const refthrottled: typeof import('@vueuse/core')['refthrottled'] const refwithcontrol: typeof import('@vueuse/core')['refwithcontrol'] const resolvecomponent: typeof import('vue')['resolvecomponent'] const resolveref: typeof import('@vueuse/core')['resolveref'] const resolveunref: typeof import('@vueuse/core')['resolveunref'] const setactivepinia: typeof import('pinia')['setactivepinia'] const setmapstoresuffix: typeof import('pinia')['setmapstoresuffix'] const shallowreactive: typeof import('vue')['shallowreactive'] const shallowreadonly: typeof import('vue')['shallowreadonly'] const shallowref: typeof import('vue')['shallowref'] const storetorefs: typeof import('pinia')['storetorefs'] const syncref: typeof import('@vueuse/core')['syncref'] const syncrefs: typeof import('@vueuse/core')['syncrefs'] const templateref: typeof import('@vueuse/core')['templateref'] const throttledref: typeof import('@vueuse/core')['throttledref'] const throttledwatch: typeof import('@vueuse/core')['throttledwatch'] const toraw: typeof import('vue')['toraw'] const toreactive: typeof import('@vueuse/core')['toreactive'] const toref: typeof import('vue')['toref'] const torefs: typeof import('vue')['torefs'] const tovalue: typeof import('vue')['tovalue'] const toggledark: typeof import('../src/composables/theme')['toggledark'] const triggerref: typeof import('vue')['triggerref'] const tryonbeforemount: typeof import('@vueuse/core')['tryonbeforemount'] const tryonbeforeunmount: typeof import('@vueuse/core')['tryonbeforeunmount'] const tryonmounted: typeof import('@vueuse/core')['tryonmounted'] const tryonscopedispose: typeof import('@vueuse/core')['tryonscopedispose'] const tryonunmounted: typeof import('@vueuse/core')['tryonunmounted'] const unref: typeof import('vue')['unref'] const unrefelement: typeof import('@vueuse/core')['unrefelement'] const until: typeof import('@vueuse/core')['until'] const useaccess: typeof import('../src/composables/access')['useaccess'] const useactiveelement: typeof import('@vueuse/core')['useactiveelement'] const useanimate: typeof import('@vueus const useasyncqueue: typeof import('@vueuse/core')['useasyncqueue'] const useasyncstate: typeof import('@vueuse/core')['useasyncstate'] const useattrs: typeof import('vue')['useattrs'] const useauthorization: typeof import('../src/composables/authorization')['useauthorization'] const usebase64: typeof import('@vueuse/core')['usebase64'] const usebattery: typeof import('@vueuse/core')['usebattery'] const usebluetooth: typeof import('@vueuse/core')['usebluetooth'] const usebreakpoints: typeof import('@vueuse/core')['usebreakpoints'] const usebroadcastchannel: typeof import('@vueuse/core')['usebroadcastchannel'] const usebrowserlocation: typeof import('@vueuse/core')['usebrowserlocation'] const usecached: typeof import('@vueuse/core')['usecached'] const useclipboard: typeof import('@vueuse/core')['useclipboard'] const usecloned: typeof import('@vueuse/core')['usecloned'] const usecolormode: typeof import('@vueuse/core')['usecolormode'] const usecompconsumer: typeof import('../src/composables/comp-consumer')['usecompconsumer'] const useconfirmdialog: typeof import('@vueuse/core')['useconfirmdialog'] const usecounter: typeof import('@vueuse/core')['usecounter'] const usecssmodule: typeof import('vue')['usecssmodule'] const usecssvar: typeof import('@vueuse/core')['usecssvar'] const usecssvars: typeof import('vue')['usecssvars'] const usecurrentelement: typeof import('@vueuse/core')['usecurrentelement'] const usecurrentroute: typeof import('../src/composables/current-route')['usecurrentroute'] const usecyclelist: typeof import('@vueuse/core')['usecyclelist'] const usedark: typeof import('@vueuse/core')['usedark'] const usedateformat: typeof import('@vueuse/core')['usedateformat'] const usedebounce: typeof import('@vueuse/core')['usedebounce'] const usedebouncefn: typeof import('@vueuse/core')['usedebouncefn'] const usedebouncedrefhistory: typeof import('@vueuse/core')['usedebouncedrefhistory'] const useelementvisibility: typeof import('@vueuse/core')['useelementvisibility'] const useemployeeseletor: typeof import('../src/hooks/employee')['useemployeeseletor'] const useglobalconfig: typeof import('../src/composables/global-config')['useglobalconfig'] const usei18n: typeof import('vue-i18n')['usei18n'] const usei18nlocale: typeof import('../src/composables/i18n-locale')['usei18nlocale'] const useidle: typeof import('@vueuse/core')['useidle'] const useimage: typeof import('@vueuse/core')['useimage'] const useinfinitescroll: typeof import('@vueuse/core')['useinfinitescroll'] const useintersectionobserver: typeof import('@vueuse/core')['useintersectionobserver'] const useinterval: typeof import('@vueuse/core')['useinterval'] const useintervalfn: typeof import('@vueuse/core')['useintervalfn'] const usekeymodifier: typeof import('@vueuse/core')['usekeymodifier'] const uselastchanged: typeof import('@vueuse/core')['uselastchanged'] const uselayoutmenu: typeof import('../src/stores/layout-menu')['uselayoutmenu'] const uselink: typeof import('vue-router')['uselink'] const useloading: typeof import('../src/composables/base-loading')['useloading'] const useloadingcheck: typeof import('../src/composables/loading')['useloadingcheck'] const uselocalstorage: typeof import('@vueuse/core')['uselocalstorage'] const usemagickeys: typeof import('@vueuse/core')['usemagickeys'] const usemanualrefhistory: typeof import('@vueuse/core')['usemanualrefhistory'] const usemediacontrols: typeof import('@vueuse/core')['usemediacontrols'] const usemediaquery: typeof import('@vueuse/core')['usemediaquery'] const usememoize: typeof import('@vueuse/core')['usememoize'] const usememory: typeof import('@vueuse/core')['usememory'] const usemessage: typeof import('../src/composables/global-config')['usemessage'] const usemetatitle: typeof import('../src/composables/meta-title')['usemetatitle'] const usemodal: typeof import('../src/composables/global-config')['usemodal'] const usemounted: typeof import('@vueuse/core')['usemounted'] const usemouse: typeof import('@vueuse/core')['usemouse'] const usemouseinelement: typeof import('@vueuse/core')['usemouseinelement'] const usemousepressed: typeof import('@vueuse/core')['usemousepressed'] const usemultitab: typeof import('../src/stores/multi-tab')['usemultitab'] const usemutationobserver: typeof import('@vueuse/core')['usemutationobserver'] const usenavigatorlanguage: typeof import('@vueuse/core')['usenavigatorlanguage'] const usenetwork: typeof import('@vueuse/core')['usenetwork'] const usenotification: typeof import('../src/composables/global-config')['usenotification'] const usenow: typeof import('@vueuse/core')['usenow'] const useobjecturl: typeof import('@vueuse/core')['useobjecturl'] const useoffsetpagination: typeof import('@vueuse/core')['useoffsetpagination'] const useonline: typeof import('@vueuse/core')['useonline'] const usepageleave: typeof import('@vueuse/core')['usepageleave'] const useparallax: typeof import('@vueuse/core')['useparallax'] const useparentelement: typeof import('@vueuse/core')['useparentelement'] const useperformanceobserver: typeof import('@vueuse/core')['useperformanceobserver'] const usepermission: typeof import('@vueuse/core')['usepermission'] const usepointer: typeof import('@vueuse/core')['usepointer'] const usepointerlock: typeof import('@vueuse/core')['usepointerlock'] const usepointerswipe: typeof import('@vueuse/core')['usepointerswipe'] const usepost: typeof import('../src/composables/api')['usepost'] const usescreenorientation: typeof import('@vueuse/core')['usescreenorientation'] const usescreensafearea: typeof import('@vueuse/core')['usescreensafearea'] const usescripttag: typeof import('@vueuse/core')['usescripttag'] const usescroll: typeof import('@vueuse/core')['usescroll'] const usescrolllock: typeof import('@vueuse/core')['usescrolllock'] const usesessionstorage: typeof import('@vueuse/core')['usesessionstorage'] const usesetglobalconfig: typeof import('../src/composables/global-config')['usesetglobalconfig'] const useshare: typeof import('@vueuse/core')['useshare'] const useslots: typeof import('vue')['useslots'] const usesorted: typeof import('@vueuse/core')['usesorted'] const usespeechrecognition: typeof import('@vueuse/core')['usespeechrecognition'] const usespeechsynthesis: typeof import('@vueuse/core')['usespeechsynthesis'] const usestepper: typeof import('@vueuse/core')['usestepper'] const usestorage: typeof import('@vueuse/core')['usestorage'] const usestorageasync: typeof import('@vueuse/core')['usestorageasync'] const usestyletag: typeof import('@vueuse/core')['usestyletag'] const usesupported: typeof import('@vueuse/core')['usesupported'] const useswipe: typeof import('@vueuse/core')['useswipe'] const usetable: typeof import('../src/hooks/pro-table')['usetable'] const usetemplaterefslist: typeof import('@vueuse/core')['usetemplaterefslist'] const usetextdirection: typeof import('@vueuse/core')['usetextdirection'] const usetextselection: typeof import('@vueuse/core')['usetextselection'] const usetextareaautosize: typeof import('@vueuse/core')['usetextareaautosize'] const usethrottle: typeof import('@vueuse/core')['usethrottle'] const usethrottlefn: typeof import('@vueuse/core')['usethrottlefn'] const usethrottledrefhistory: typeof import('@vueuse/core')['usethrottledrefhistory'] const usetimeago: typeof import('@vueuse/core')['usetimeago'] const usetimeout: typeof import('@vueuse/core')['usetimeout'] const usetimeoutfn: typeof import('@vueuse/core')['usetimeoutfn'] const usetimeoutpoll: typeof import('@vueuse/core')['usetimeoutpoll'] const usetimestamp: typeof import('@vueuse/core')['usetimestamp'] const usetitle: typeof import('@vueuse/core')['usetitle'] const usetonumber: typeof import('@vueuse/core')['usetonumber'] const usetostring: typeof import('@vueuse/core')['usetostring'] const usetoggle: typeof import('@vueuse/core')['usetoggle'] const usetools: typeof import('../src/hooks/self-service')['usetools'] const usetransition: typeof import('@vueuse/core')['usetransition'] const useurlsearchparams: typeof import('@vueuse/core')['useurlsearchparams'] const useusermanager: typeof import('../src/hooks/user')['useusermanager'] const useusermedia: typeof import('@vueuse/core')['useusermedia'] const useuserstore: typeof import('../src/stores/user')['useuserstore'] const usevmodel: typeof import('@vueuse/core')['usevmodel'] const usevmodels: typeof import('@vueuse/core')['usevmodels'] const usevibrate: typeof import('@vueuse/core')['usevibrate'] const usevirtuallist: typeof import('@vueuse/core')['usevirtuallist'] const usewakelock: typeof import('@vueuse/core')['usewakelock'] const usewebnotification: typeof import('@vueuse/core')['usewebnotification'] const usewebsocket: typeof import('@vueuse/core')['usewebsocket'] const usewebworker: typeof import('@vueuse/core')['usewebworker'] const usewebworkerfn: typeof import('@vueuse/core')['usewebworkerfn'] const usewindowfocus: typeof import('@vueuse/core')['usewindowfocus'] const usewindowscroll: typeof import('@vueuse/core')['usewindowscroll'] const usewindowsize: typeof import('@vueuse/core')['usewindowsize'] const watch: typeof import('vue')['watch'] const watcharray: typeof import('@vueuse/core')['watcharray'] const watchatmost: typeof import('@vueuse/core')['watchatmost'] const watchdebounced: typeof import('@vueuse/core')['watchdebounced'] const watchdeep: typeof import('@vueuse/core')['watchdeep'] const watcheffect: typeof import('vue')['watcheffect'] const watchignorable: typeof import('@vueuse/core')['watchignorable'] const watchimmediate: typeof import('@vueuse/core')['watchimmediate'] const watchonce: typeof import('@vueuse/core')['watchonce'] const watchpausable: typeof import('@vueuse/core')['watchpausable'] const watchposteffect: typeof import('vue')['watchposteffect'] const watchsynceffect: typeof import('vue')['watchsynceffect'] const watchthrottled: typeof import('@vueuse/core')['watchthrottled'] const watchtriggerable: typeof import('@vueuse/core')['watchtriggerable'] const watchwithfilter: typeof import('@vueuse/core')['watchwithfilter'] const whenever: typeof import('@vueuse/core')['whenever'] } // for type re-export declare global { // @ts-ignore export type { component, componentpublicinstance, computedref, injectionkey, proptype, ref, vnode, writablecomputedref } from 'vue' }
使用
通过如上步骤配置好了之后,我们接下来就可以直接使用这些自动导入的依赖库了,代码如下:
// test.vue
const count = ref(0) const doubled = computed(() => count.value * 2)
如上,我们就不需要再重新导入ref
了。
到此这篇关于vite使用unplugin-auto-import实现vue3中的自动导入的文章就介绍到这了,更多相关vue3中unplugin-auto-import内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论