Vue3+vite优化基础架构(1)--- 使用unplugin-vue-components

devtools/2024/10/22 18:33:03/

Vue3+vite优化基础架构(1)--- 使用unplugin-vue-components

  • 说明
  • 安装unplugin-vue-components
  • vite.config.js中使用unplugin-vue-components/vite

说明

这里记录下自己在Vue3+vite的项目使用unplugin-vue-components/vite来自定义组件自动全局引入svg雪碧图组件,不使用ts语法,方便以后直接使用。这里承接自己的博客Vue3+vite搭建基础架构(11)— 菜单栏功能和Tab页功能实现这篇博客,在该博客项目的基础上增加使用unplugin-vue-components/vite来使组件全局引用。

unplugin-vue-components官网:https://github.com/antfu/unplugin-vue-components#readme

vuecomponents_8">安装unplugin-vue-components

命令如下,-D表示该依赖添加在package.json里面的devDependencies。

devDependencies下的依赖包,只是我们在本地或开发坏境下运行代码所依赖的,若发到线上,其实就不需要devDependencies下的所有依赖包;(比如各种loader,babel全家桶及各种webpack的插件等)只用于开发环境,不用于生产环境,因此不需要打包。

javascript">npm install unplugin-vue-components -D

在webstorm里面的Terminal输入npm install unplugin-vue-components -D命令安装该依赖。执行完如下:
在这里插入图片描述
package.json会增加unplugin-vue-components版本号
在这里插入图片描述

vuecomponentsvite_20">vite.config.js中使用unplugin-vue-components/vite

官网提供的配置属性如下:

javascript">Components({// relative paths to the directory to search for components.dirs: ['src/components'],// valid file extensions for components.extensions: ['vue'],// Glob patterns to match file names to be detected as components.// When specified, the `dirs` and `extensions` options will be ignored.globs: ['src/components/*.{vue}'],// search for subdirectoriesdeep: true,// resolvers for custom componentsresolvers: [],// generate `components.d.ts` global declarations,// also accepts a path for custom filename// default: `true` if package typescript is installeddts: false,// Allow subdirectories as namespace prefix for components.directoryAsNamespace: false,// Collapse same prefixes (camel-sensitive) of folders and components// to prevent duplication inside namespaced component name.// works when `directoryAsNamespace: true`collapseSamePrefixes: false,// Subdirectory paths for ignoring namespace prefixes.// works when `directoryAsNamespace: true`globalNamespaces: [],// auto import for directives// default: `true` for Vue 3, `false` for Vue 2// Babel is needed to do the transformation for Vue 2, it's disabled by default for performance concerns.// To install Babel, run: `npm install -D @babel/parser`directives: true,// Transform path before resolvingimportPathTransform: v => v,// Allow for components to override other components with the same nameallowOverrides: false,// filters for transforming targetsinclude: [/\.vue$/, /\.vue\?vue/],exclude: [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/],// Vue version of project. It will detect automatically if not specified.// Acceptable value: 2 | 2.7 | 3version: 2.7,// Only provide types of components in library (registered globally)types: []
})

在vite.config.js添加如下代码:
在这里插入图片描述
代码如下:

javascript">import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
//引入path用于写别名配置,自带无须安装
import path from 'path'
//使用svg-icons插件
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
//使用vite-plugin-vue-setup-extend
import VueSetupExtend from 'vite-plugin-vue-setup-extend'
//使用unplugin-vue-components/vite按需加载自定义组件
import Components from 'unplugin-vue-components/vite'// https://vitejs.dev/config/
export default defineConfig({plugins: [vue(),// 注册所有的svg文件生成svg雪碧图createSvgIconsPlugin({iconDirs: [path.resolve(process.cwd(), "src/assets/icons")], //svg图片存放的目录symbolId: "icon-[name]", // symbol的idinject: "body-last", // 插入的位置customDomId: "__svg__icons__dom__" // svg的id}),VueSetupExtend(),// 全部属性如下,这里只使用部分需要的Components({dirs: ['src/components'], // 指定组件位置,默认指定文件夹是src/componentsextensions: ['vue'], // 组件的有效文件扩展名deep: true, // 搜索子目录dts: 'src/components/components.d.ts', //配置文件生成位置,默认情况下启用,如果为true,表示默认生成到根目录文件下,false为不生成该文件,这里我没有安装ts,生成ts文件发现并没有报错,测试打包发现也没有报错//resolvers: [], // 自定义组件的解析器,可以将ElementPlus组件改为到这个里面使用//directoryAsNamespace: false, // 允许子目录作为组件的命名空间前缀//globalNamespaces: [], // 忽略命名空间前缀的子目录路径,当directoryAsNamespace: true 时有效//directives: true, //需要 Babel 来为 Vue 2 进行转换,出于性能考虑,它默认处于禁用状态。//include: [/.vue$/, /.vue?vue/],//exclude: [/[\/]node_modules[\/]/, /[\/].git[\/]/, /[\/].nuxt[\/]/],})],resolve: {//别名配置alias: {'~': path.resolve(__dirname, './'),'@': path.resolve(__dirname, 'src')},//引入文件的时候,可以忽略掉以下文件后缀extensions: ['.js', '.mjs', '.vue', '.json', '.less', '.css']},css:{//预处理器配置项preprocessorOptions:{less:{//支持直接使用表达式 width: 100px - 20px;得到值为width:80px;math: "always"}}}
})

启动项目后才会生成components.d.ts,文件位置如下:
在这里插入图片描述
删除掉之前项目中以下代码和文件,使用unplugin-vue-components/vite来代替全局组件的使用。
在这里插入图片描述
浏览器结果如下,图标正常显示出来,与之前一模一样:
在这里插入图片描述

到这里使用unplugin-vue-components/vite来使svg-icon组件进行全局引用的目的就达成了,后面如果还需要添加其他组件,直接在/src/components下添加相应组件文件即可。它会自动添加到components.d.ts文件里面。
注意:如果你把项目中/src/components下的svg-icon的组件文件夹给删除了,components.d.ts里面并不会自动删除SvgIcon: typeof import(‘./svg-icon/index.vue’)[‘default’]这句代码,需要自己手动进行删除。


http://www.ppmy.cn/devtools/20235.html

相关文章

OceanBase 分布式数据库【信创/国产化】- OceanBase 与 MySql 兼容性对比

本心、输入输出、结果 文章目录 OceanBase 分布式数据库【信创/国产化】- OceanBase 与 MySql 兼容性对比前言OceanBase 数据更新架构OceanBase 与 MySql 兼容性对比OceanBase 数据类型SQL 语法过程性语言OceanBase 支持的字符集OceanBase 存储引擎OceanBase 分区支持OceanBase…

C++orm使用插曲——MySQL保留字

近日开始对数据库开始涉猎,在编写数据库操作类调试过程中遇到一个问题: mysql> select * from environmental_variable_table where keytemperature AND dev_ip192.168.1.1; 1064 - You have an error in your SQL syntax; check the manual that co…

k8s部署alertmanager

修改alertmanager-pvc.yaml文件中的信息&#xff0c;然后应用YAML文件 cat > /opt/k8s/alertmanager/alertmanager-pvc.yaml <<EOF apiVersion: v1 kind: PersistentVolumeClaim metadata:name: alertmanager-data-pvc spec:accessModes:- ReadWriteManystorageClass…

记一次线上日志堆栈不打印问题排查(附:高并发系统日志打印方案可收藏)

目录 一.线上的日志堆栈不打印了二.一步一步仔细排查三.最后搞定四.聊一聊线上日志到底应该怎么打印4.1 日志打印的诉求4.2 常见的系统日志上报方案4.2.1 ELK 方案4.2.2 自定义log appender 完成应用日志采集. 4.3 日志常见框架傻傻分不清4.4 日志在高并发系统中需要注意的 tip…

STM32之串口中断接收丢失数据

五六年没搞STM32了&#xff0c;这个项目一切都挺顺利&#xff0c;万万没想到被串口接收中断恶心到了。遇到的问题很奇怪 HAL_UART_Receive_IT(&huart1, &rx_buffer[rx_index], LCD_UART_LEN); 这个代码中 LCD_UART_LEN1的时候&#xff0c;接收过来的数据&#xff0c;数…

(学习日记)2024.05.03:UCOSIII第五十七节:User文件夹函数概览(uCOS-III->Source文件夹)第三部分

写在前面&#xff1a; 由于时间的不足与学习的碎片化&#xff0c;写博客变得有些奢侈。 但是对于记录学习&#xff08;忘了以后能快速复习&#xff09;的渴望一天天变得强烈。 既然如此 不如以天为单位&#xff0c;以时间为顺序&#xff0c;仅仅将博客当做一个知识学习的目录&a…

认识HTTP

HTTP缺点 通信使用明文&#xff08;不加密&#xff09;&#xff0c;内容可能会被窃听 不验证通信方的身份&#xff0c;可能遭遇伪装 无法证明报文的完整性&#xff0c;所以有可能遭篡改 一、通信使用明文&#xff08;不加密&#xff09;&#xff0c;内容可能会被窃听 TCP/…

每日JAVA高级面试题

Java 高级面试问题及答案 以下是一些在Java高级面试中可能会遇到的问题&#xff0c;以及对这些问题的探讨和回答。 问题 1: Java内存模型是什么&#xff1f;请解释其重要性。 探讨&#xff1a; Java内存模型&#xff08;Java Memory Model, JMM&#xff09;是Java虚拟机&…