当前位置: 首页 > news >正文

Vue封装全局SVG组件

1.SVG图标配置

1.安装插件

npm install vite-plugin-svg-icons -D

2.Vite.config.ts中配置

import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'
export default () => {return {plugins: [createSvgIconsPlugin({// Specify the icon folder to be cachediconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],// Specify symbolId formatsymbolId: 'icon-[dir]-[name]',}),],}
}

3.main.ts中导入

//SVG插件必须的配置
import 'virtual:svg-icons-register'

4.组件内使用

1.下载svg代码

阿里巴巴图标库或者其他图标库下载SVG代码,复制到对应的文件中

2.SVG使用
<template><div><!-- svg:图标的外层节点,内部需要于use标签结合使用 --><svg style="width: 30px; height: 30px;"><!-- xlink:href执行用的图标,属性务必是#icon-图标名字 --><!-- use标签的fill属性为图标的颜色填充 --><use xlink:href="#icon-base" fill="blue"></use></svg></div>
</template><script setup lang="ts"></script><style scoped lang="scss"></style>

2.SVG组件封装

1.创建组件文件

2.封装组件 

<template><svg :style="{ width, height }"><use :xlink:href="prefix + name" :fill="color"></use></svg>
</template><script setup lang="ts">
defineProps({//xlink:href属性值的前缀prefix: {type: String,default: '#icon-'},//svg矢量图的名字name: String,//svg图标的颜色color: {type: String,default: ''},//svg宽度width: {type: String,default: '16px'},//svg高度height: {type: String,default: '16px'}
})
</script><style scoped lang="scss"></style>

3.在其他组件使用

<template><div><SvgIcon name="base" width="30px" height="30px" color="blue"></SvgIcon></div>
</template><script setup lang="ts">
import SvgIcon from '@/components/Svg/index.vue'
</script><style scoped lang="scss"></style>

3.SVG组件注册为全局组件

1.创建文件

2.注册全局组件

//引入全局组件
import SvgIcon from './SvgIcon/index.vue';
import type { App, Component } from 'vue';
//全局对象
const components: { [name: string]: Component } = { SvgIcon };
//对外暴露插件对象
export default {//insatll方法install(app: App) {//注册项目为全局组件(可注册多个)Object.keys(components).forEach((key: string) => {//注册全局组件app.component(key, components[key]);})}
}

3.引入到main.ts

import gloablComponent from './components/index';
app.use(gloablComponent);

4.组件中使用

<template><div><SvgIcon name="base" width="30px" height="30px" color="blue"></SvgIcon></div>
</template><script setup lang="ts"></script><style scoped lang="scss"></style>

http://www.lryc.cn/news/178777.html

相关文章:

  • 课题学习(二)----倾角和方位角的动态测量方法(基于磁场的测量系统)
  • Docker-Windows安装使用
  • 在Windows11上安装ubuntu虚拟机
  • 【微服务】spring 控制bean加载顺序使用详解
  • python-切换镜像源和使用PyCharm进行第三方开源包安装
  • tp6 + swagger 配置文档接口
  • 试图一文彻底讲清 “精准测试”
  • Visual Studio 删除行尾空格
  • LeetCode_BFS_中等_1926.迷宫中离入口最近的出口
  • 开源Windows12网页版HTML源码
  • vscode中使用指定路径下的cmake
  • 复杂度分析
  • Linux安装jrockit-jdk1.6.0_29-R28.2.0-4.1.0-linux-x64
  • 7.2 怎样定义函数
  • Chrome扩展V2到V3的变化
  • lock、tryLock、lockInterruptibly有什么区别?
  • mysql面试题5:索引、主键、唯一索引、联合索引的区别?什么情况下设置了索引但无法使用?并且举例说明
  • 数据集笔记:纽约花旗共享单车od数据
  • 为什么 0.1+0.2 不等于 0.3
  • huggingface_hub v0.17 现已发布
  • 机器学习——一元线性回归构造直线,并给出损失函数
  • OpenHarmony自定义组件介绍
  • 云原生之使用Docker部署PDF多功能工具Stirling-PDF
  • B树和B+树的介绍和对比,以及MySQL为何选择B+树
  • MD5 绕过第一式:弱比较绕过
  • 红黑树是如何实现的?
  • 实验室没人导该怎么办
  • pytest简明教程
  • 解决方案:解决https页面加载http资源报错
  • 嵌入式开源库之libmodbus学习笔记