17 【vue3自动导入配置】
1.自动导入组件库组件
unplugin-vue-components
安装
pnpm install unplugin-vue-components -D
然后将下面的代码添加到 Vite 的配置文件。
Vite配置
import { defineConfig } from 'vite'
import Components from 'unplugin-vue-components/vite'
import {
ElementPlusResolver,
AntDesignVueResolver,
VantResolver,
HeadlessUiResolver,
ElementUiResolver
} from 'unplugin-vue-components/resolvers'
export default defineConfig({
plugins: [
Components({
resolvers: [
ElementPlusResolver(),
AntDesignVueResolver(),
VantResolver(),
HeadlessUiResolver(),
ElementUiResolver()
]
})
]
})
插件会生成一个ui库组件以及指令路径components.d.ts文件,详情看这个vue3的issue types(defineComponent): support for expose component types
declare module 'vue' {
export interface GlobalComponents {
ElAside: typeof import('element-plus/es')['ElAside']
ElButton: typeof import('element-plus/es')['ElButton']
ElContainer: typeof import('element-plus/es')['ElContainer']
ElDropdown: typeof import('element-plus/es')['ElDropdown']
ElDropdownItem: typeof import('element-plus/es')['ElDropdownItem']
ElDropdownMenu: typeof import('element-plus/es')['ElDropdownMenu']
ElHeader: typeof import('element-plus/es')['ElHeader']
ElIcon: typeof import('element-plus/es')['ElIcon']
ElMain: typeof import('element-plus/es')['ElMain']
ElMenu: typeof import('element-plus/es')['ElMenu']
ElMenuItem: typeof import('element-plus/es')['ElMenuItem']
ElResult: typeof import('element-plus/es')['ElResult']
}
}
export { }
想了解其他的打包工具(Rollup, Vue CLI),请参考 unplugin-vue-components。
自动导入自己的组件
直接写组件名即可,插件会帮你引入进来 注意别重名
import { defineConfig } from 'vite'
import Components from 'unplugin-vue-components/vite'
export default defineConfig({
plugins: [
Components({
dirs: ['src/components', 'src/views'],
extensions: ['vue'],
dts: 'src/components.d.ts'
})
]
})
插件会生成一个自己组件路径的components.d.ts文件,详情看这个vue3的issue types(defineComponent): support for expose component types
declare module 'vue' {
export interface GlobalComponents {
BaseFilter: typeof import('./components/Common/BaseFilter.vue')['default']
BaseHeader: typeof import('./components/Common/BaseHeader.vue')['default']
BasePagination: typeof import('./components/Common/BasePagination.vue')['default']
BaseSidebar: typeof import('./components/Common/BaseSidebar.vue')['default']
BaseTags: typeof import('./components/Common/BaseTags.vue')['default']
BaseTitle: typeof import('./components/Common/BaseTitle.vue')['default']
}
}
export { }
2.自动导入Vue 相关函数
unplugin-auto-import/vite
自动导入vue3的hooks,借助unplugin-auto-import/vite这个插件
支持vue , vue-router , vue-i18n , @vueuse/head , @vueuse/core 等自动引入
安装
pnpm i -D unplugin-auto-import
Vite配置
import { defineConfig } from 'vite'
import AutoImport from 'unplugin-auto-import/vite'
export default defineConfig({
plugins: [
AutoImport({
imports: ['vue', 'vue-router', ,'vue-i18n', '@vueuse/head', '@vueuse/core',],
dts: 'src/auto-import.d.ts'
})
]
})
原理: 安装的时候会自动生成auto-imports.d文件(默认是在根目录)
declare global {
const ref: typeof import('vue')['ref']
const reactive: typeof import('vue')['reactive']
const computed: typeof import('vue')['computed']
const createApp: typeof import('vue')['createApp']
const watch: typeof import('vue')['watch']
const customRef: typeof import('vue')['customRef']
const defineAsyncComponent: typeof import('vue')['defineAsyncComponent']
.
.
.
}
export {}
可以选择auto-import.d.ts生成的位置,使用ts建议设置为src/auto-import.d.ts
其他插件 vue-router , vue-i18n , @vueuse/head , @vueuse/core 等自动引入的自动引入请查看文档
全局引入axios
AutoImport({
imports: [
'vue',
'vue-router',
{
axios: [
['default', 'axios'],
],
},
'@vueuse/core',
],
}),
3.自动导入组件库样式
import { createStyleImportPlugin, ElementPlusResolve } from 'vite-plugin-style-import';
plugins: [vue(),
createStyleImportPlugin({
resolves: [ElementPlusResolve()],
}),
],
4.自动导入icon
UnoCSS 是 antfu 开发的一个原子化 CSS 引擎,这里就不详细说了,大家感兴趣可以去读一下他的文章【重新构想原子化 CSS】。
安装依赖
pnpm i -D unocss @iconify-json/ant-design
修改 vite.config.ts 配置
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import Unocss from 'unocss/vite'
import {
presetAttributify,
presetIcons,
presetUno,
transformerDirectives,
transformerVariantGroup,
} from 'unocss';
export default defineConfig({
plugins: [
vue(),
Unocss({
presets: [
presetUno(),
presetAttributify(),
presetIcons({
scale: 1.2,
warn: true,
}),
],
transformers: [transformerDirectives(), transformerVariantGroup()],
})
]
})
修改 main.ts
import 'uno.css'
使用
// svg图片
<i
class="i-ant-design-picture-filled w-330px h-240px"
/>
// 图标字体
<i
i="ant-design-picture-filled"
/>
|