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

vue项目使用svg图标

下面是在 Vue 3 项目中完整引入和使用 vite-plugin-svg-icons 的步骤

1、安装插件

npm install vite-plugin-svg-icons -D
# 或
yarn add vite-plugin-svg-icons -D
# 或
pnpm add vite-plugin-svg-icons -D

2、配置 Vite

在 vite.config.ts 或 vite.config.js 中配置:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'export default defineConfig({plugins: [vue(),createSvgIconsPlugin({// 指定需要缓存的图标文件夹iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')],// 指定symbolId格式symbolId: 'icon-[name]',// 可选配置svgoOptions: {plugins: [{ name: 'removeAttrs', params: { attrs: ['fill'] } } // 移除svg默认颜色]}})]
})

3. 创建 SVG 组件

<script setup>
import { computed } from 'vue'const props = defineProps({name: {type: String,required: true,},className: {type: String,default: '',},size: {type: [Number, String],default: 15,},circle: {type: Boolean,default: false,},color: String,defaultImg: String,
})
const style = computed(() => {const size = typeof props.size === 'string' ? props.size : `${props.size}px`return {width: size,height: size,borderRadius: props.circle ? '50%' : null,color: props.color,}
})
const symbolId = computed(() => `#icon-${props.name}`)
</script>
<template><svg aria-hidden="true" class="svg-icon" :class="className" :style="style"><use :xlink:href="symbolId" /></svg>
</template>
<style scoped>
.svg-icon {/* width: 30px;height: 30px; */display: inline-block;vertical-align: -2px;fill: currentColor;
}
</style>

4. 全局注册组件

在 main.js 中:

import { createApp } from 'vue'
import App from './App.vue'
import 'virtual:svg-icons-register' // 引入注册脚本
import SvgIcon from '@/components/SvgIcon.vue' // 引入组件const app = createApp(App)// 全局注册svg组件
app.component('SvgIcon', SvgIcon)app.mount('#app')

5. 使用图标

  1. 将 SVG 文件放入 src/assets/svg 目录

  2. 在组件中使用:

    <div class="img-list" v-for="(item, index) in iconlist" :key="index" @click="itemClick(item)">
    <div class="default-img"><!-- 在组件中使用 --><SvgIcon :name="item.iconname" size="30" class="svg-icon" />
    </div>
    </div>
    const list = ref([{iconname: 'float-robot',id: 1},{iconname: 'float-wx',id: 2},{iconname: 'float-tell',id: 3},{iconname: 'float-qq',id: 4},{iconname: 'float-message-board',id: 5}
    ])
    <style scoped lang="scss">
    .img-list{padding: 20px;background: #fff;display: flex;flex-direction: column;justify-content: space-between;justify-items: center;height: 330px;cursor: pointer;box-shadow: 0px 0px 14px 0px rgba(0, 0, 0, 0.12);border-radius: 34px;box-sizing: border-box;&:hover {.default-img {.svg-icon {color: #005fff; // 默认颜色fill: currentColor; // 继承color颜色transition: color 0.3s ease; // 添加过渡效果}}}
    }
    </style>
    

    最后看效果:鼠标经过svg图标变蓝色

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

相关文章:

  • 智能网卡之hinic3 WQE(Work Queue Element)结构梳理
  • go的工具库:github.com/expr-lang/expr
  • 力扣HOT100之二分查找:4. 寻找两个正序数组的中位数
  • PyTorch——损失函数与反向传播(8)
  • macOS 升级 bash 到最新版本
  • Linux下如何查看一个端口被什么进程占用? 该进程又打开了哪些文件?
  • 力扣面试150题--课程表
  • 用通俗的话解释下MCP是个啥?
  • LeetCode 高频 SQL 50 题(基础版)之 【子查询】· 上
  • Spark流水线+Gravitino+Marquez数据血缘采集
  • 一个完整的时间序列异常检测系统,使用Flask作为后端框架,实现了AE(自编码器)、TimesNet和LSTM三种模型,并提供可视化展示
  • 深度学习在非线性场景中的核心应用领域及向量/张量数据处理案例,结合工业、金融等领域的实际落地场景分析
  • 基于微信小程序的车位共享平台的设计与实现源码数据库文档
  • 多模态大语言模型arxiv论文略读(111)
  • 网页端 VUE+C#/FastAPI获取客户端IP和hostname
  • 一个自动反汇编脚本
  • 函数与数列的交汇融合
  • 怎么让自己ip显示外省?一文说清操作
  • 【Docker】容器安全之非root用户运行
  • 汽车车载软件平台化项目规模颗粒度选择的一些探讨
  • 【八股消消乐】构建微服务架构体系—服务注册与发现
  • 大数据+智能零售:数字化变革下的“智慧新零售”密码
  • C++_核心编程_菱形继承
  • 掌握Git核心:版本控制、分支管理与远程操作
  • c#,Powershell,mmsys.cpl,使用Win32 API展示音频设备属性对话框
  • STM标准库-TIM旋转编码器
  • 深入解析JVM工作原理:从字节码到机器指令的全过程
  • MCP通信方式之Streamable HTTP
  • 第七十三篇 从电影院售票到停车场计数:生活场景解析Java原子类精髓
  • 【原创】基于视觉模型+FFmpeg+MoviePy实现短视频自动化二次编辑+多赛道