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

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

安装unplugin-vue-components

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

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

npm install unplugin-vue-components -D

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

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

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

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添加如下代码:
在这里插入图片描述
代码如下:

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.hqwc.cn/news/650404.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

树莓派也能用于心脏病数据安全管理!

树莓派(Raspberry Pi)是 EKORA 用于病人数据安全管理的用户友好型系统的核心。 为心脏起搏器和除颤器患者提供的纸质记录效率低下,给有效护理带来了障碍。两位英国国家医疗服务系统(NHS)的心脏病专家建立了 EKORA&…

Python3.11修改并运行oneforall

遇到的问题 使用python3.11默认无法运行oneforall脚本,出现如下报错 # 解决方案 修改 /usr/local/lib/python3.11/dist-packages/exrex.py exrex.py具体文件路径报错中会显示 vim /usr/local/lib/python3.11/dist-packages/exrex.py# 修改前 from re import sre…

【软件测试】终于有人讲明白:bug的分类和定级了!

01、bug的定义 一般是指不满足用户需求的则可以认为是bug,狭义指软件程序的漏洞或缺陷,广义指测试工程师或用户提出的软件可改进的细节、或与需求文档存在差异的功能实现等 对应三个测试目的: 为了发现程序的代码或业务逻辑错误 为了检查产…

数字藏品:重塑艺术与科技的新媒介

数字藏品,这个新兴的词汇,正在逐渐渗透到我们的日常生活中。它不仅是一种新的艺术表达方式,更是一种科技与艺术相结合的全新媒介。那么,数字藏品究竟是什么呢? 首先,我们需要明确一点,数字藏品并…

Redis底层数据结构之quicklist

目录 一、概述二、quicklist结构三、quicklistNode结构四、优缺点 redis底层数据结构已完结👏👏👏: ☑️redis底层数据结构之SDS☑️redis底层数据结构之ziplist☑️redis底层数据结构之quicklist☑️redis底层数据结构之Dict☑️…

jmeter安装和简单使用

jmeter安装和简单使用 1. 安装 jmeter是基于Java开发的测试应用,其运行依赖于java环境,所以在下载运行jmeter前,先确保本机已经安装jdk或者jre,安装jdk步骤此处不描述。 下载jmeter: jmeter是Apache旗下的产品&…

获取boss直聘城市地区josn数据

获取boss直聘城市地区josn数据 当我需要爬取多个城市的地区的时候,只能手动点击,然后一个一个看 结果: 能看到所有区域所有子地区的地区代码 解析该JSON数据 import pandas as pd import requests code[] area[] 城市代码101210100 res…

广工电工与电子技术实验报告-8路彩灯循环控制电路

实验代码 module LED_water (clk,led); input clk; output [7:0] led; reg [7:0] led; integer p; reg clk_1Hz; reg [7:0] current_state, next_state; always (posedge clk) begin if(p25000000-1)begin …

详解23种设计模式——工厂模式

工厂模式 | CoderMast编程桅杆工厂模式 设计思想 工厂模式是最常用的设计模式之一,属于创建型模式,将创建对象的权利交给了一个工厂类,从而提供了一种不使用构造方法的情况下创建对象的途径,无需指定要创建的具体类,将…

windows服务器iis系统部署https

源地址:https://www.ctvol.com/seoomethods/1418785.html https是网页常用的一种网络安全机制,在部署其他服务器https,我们在前面文章中已经提到过。下面我们来说说windows服务器iis系统部署https步骤: 1、到服务提供商下载所需…

4月25日 C++day4

#include <iostream> using namespace std;class Person {const string name;int age;char sex; public:Person():name("lisi"){cout << "Person无参构造" << endl;}Person(string name,int age,char sex):name(name),age(age),sex(sex)…

vue项目打包时因为图片问题报错

执行 npm run build命令打包项目时报错&#xff0c;看起来是图片的问题&#xff1a; package.json里面image-webpack-loader的版本是^7.0.1 解决方案&#xff1a; 1、先卸载 npm uninstall image-webpack-loader 2、用cnpm重新安装 cnpm install image-webpack-loader --save…