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

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.xdnf.cn/news/12872.html

相关文章:

  • 软件工程的软件生命周期通常分为以下主要阶段
  • 计算机网络基础总结:TCP/IP 模型、TCP vs UDP、DNS 查询过程
  • React、Git、计网、发展趋势等内容——前端面试宝典(字节、小红书和美团)
  • Vue项目PDF目录功能集成【一】——方案深度思考
  • Android 线性布局中常见的冲突属性总结
  • 在网络排错中,经常会用到的操作命令和其作用
  • 剑指offer19_链表中倒数第k个节点
  • Jmeter(四) - 如何在jmeter中创建网络测试计划
  • protues仿真+C51+外部中断
  • MATLAB生成大规模无线通信网络拓扑(任意节点数量)
  • 微服务体系下将环境流量路由到开发本机
  • spring中的@KafkaListener 注解详解
  • NLP学习路线图(三十四): 命名实体识别(NER)
  • unity实现自定义粒子系统
  • java 时区时间转为UTC
  • 云原生架构赋能企业数字化转型:从理念到落地的系统性探索
  • springboot启动mapper找不到方法对应的xml
  • 【Redis/2】核心特性、应用场景与安装配置
  • 用于小目标检测的归一化高斯Wasserstein距离(NWD)之论文阅读
  • 国家奖学金答辩PPT+文稿
  • Halo站点全站定时备份并通过邮箱存储备份
  • 【C++】25. 哈希表封装unordered_map和unordered_set
  • Ubuntu系统多网卡多相机IP设置方法
  • 【Redis技术进阶之路】「原理分析系列开篇」分析客户端和服务端网络诵信交互实现(服务端执行命令请求的过程 - 初始化服务器)
  • MCP笔记:介绍和原理
  • Web3 借贷与清算机制全解析:链上金融的运行逻辑
  • 基于安卓的线上考试APP源码数据库文档
  • MAC-安装Homebrew、安装Git
  • c++ decltype关键字
  • 二叉数-100.相同的树-力扣(LeetCode)