Vue3+Vite 实现引入svg图标

页面展示效果图如下:

在这里插入图片描述

1. 安装依赖插件 vite-plugin-svg-icons

npm i vite-plugin-svg-icons

2. 安装 fast-glob

npm i fast-glob

3. 配置 vite.config.js(或vite.config.ts)
import path from "path";
import {createSvgIconsPlugin} from 'vite-plugin-svg-icons'; 

export default defineConfig({
  plugins: [
    createSvgIconsPlugin({
      // 需要自动导入的 svg 文件目录(可自行修改)我的路径如下图所示
      iconDirs: [path.resolve(process.cwd(), "src/svgIcon/svg")],
      // 执行icon name的格式(可自行修改)
      symbolId: "icon-[dir]-[name]",
    })
  ],
})

在这里插入图片描述

4. 在 main.js(或main.ts) 入口注册脚本

import "virtual:svg-icons-register";
注意: vite-plugin-svg-icons 插件注册,不引入会导致svg图标无法正常显示

5. 封装 SvgIcon 组件
<template>
	<svg :style="iconStyle" aria-hidden="true">
		<use :xlink:href="symbolId" :fill="color"/>
	</svg>
</template>

<script setup lang="ts" name="SvgIcon">
import { computed, CSSProperties } from "vue";

interface SvgProps {
	name: string; // 图标的名称(Svg 文件名) ==> 必传
	prefix?: string; // 图标的前缀 ==> 非必传(默认为"icon")
	iconStyle?: CSSProperties; // 图标的样式 ==> 非必传
	color:string
}
// 接收父组件参数并设置默认值
const props = withDefaults(defineProps<SvgProps>(), {
	prefix: "icon",
	iconStyle: () => ({ width: "30px", height: "30px"}),
	color:"#333"
});

const symbolId = computed(() => `#${props.prefix}-${props.name}`);
</script>
6. main.js(或main.ts)引入svg组件并注册

import svgIcon from './svgIcon/index.vue'
import "virtual:svg-icons-register";
app.component('svg-icon',svgIcon)

7.组件中使用
<svg-icon name="订单" color="pink" iconStyle="{width:40px;height:40px}"></svg-icon>
<svg-icon name="chanpinshezhi" color="skyblue" iconStyle="{width:40px;height:40px}"></svg-icon>
踩坑:svg-icon图片不能改颜色,svg-icon 为啥变不了颜色?

因为字体图标库下载的时候svg文件里自带了fill属性,所以用color改不了svg的颜色。

解决方法:改变svg-icon图片代码中的fill属性的值(常用)

将所有的fill属性的颜色填充值,fill="xxxx"改为fill=“currentColor” 或者删除fill属性

在这里插入图片描述

以下是一个基于 Vue3 和 Vite实现拖拽产生连线的代码示例: ```html <template> <div class="container"> <div class="box" v-for="(item, index) in boxes" :key="item.id" :style="{ top: item.top + 'px', left: item.left + 'px' }" @mousedown="mousedown(index)"> {{ item.name }} </div> <svg class="line-container"> <line v-for="(line, index) in lines" :key="index" :x1="line.x1" :y1="line.y1" :x2="line.x2" :y2="line.y2" /> </svg> </div> </template> <script> import { reactive, toRefs } from 'vue' export default { setup() { const state = reactive({ boxes: [ { id: 1, name: 'Box 1', top: 100, left: 100 }, { id: 2, name: 'Box 2', top: 200, left: 200 }, { id: 3, name: 'Box 3', top: 300, left: 300 }, ], lines: [], isDragging: false, currentBoxIndex: null, startX: null, startY: null, }) const mousedown = (index) => { state.isDragging = true state.currentBoxIndex = index state.startX = state.boxes[index].left state.startY = state.boxes[index].top } const mousemove = (event) => { if (state.isDragging) { const box = state.boxes[state.currentBoxIndex] box.left = state.startX + event.clientX - state.startX - box.width / 2 box.top = state.startY + event.clientY - state.startY - box.height / 2 } } const mouseup = () => { state.isDragging = false state.currentBoxIndex = null } const connectBoxes = () => { const lastBox = state.boxes[state.boxes.length - 1] const secondLastBox = state.boxes[state.boxes.length - 2] state.lines.push({ x1: secondLastBox.left + secondLastBox.width / 2, y1: secondLastBox.top + secondLastBox.height / 2, x2: lastBox.left + lastBox.width / 2, y2: lastBox.top + lastBox.height / 2, }) } return { ...toRefs(state), mousedown, mousemove, mouseup, connectBoxes, } }, mounted() { document.addEventListener('mousemove', this.mousemove) document.addEventListener('mouseup', this.mouseup) }, beforeUnmount() { document.removeEventListener('mousemove', this.mousemove) document.removeEventListener('mouseup', this.mouseup) }, } </script> <style> .container { position: relative; height: 500px; } .box { position: absolute; width: 100px; height: 100px; background-color: #ccc; text-align: center; line-height: 100px; user-select: none; cursor: move; } .line-container { position: absolute; width: 100%; height: 100%; pointer-events: none; } </style> ``` 代码中实现了一个简单的拖拽产生连线的功能,具体实现方式如下: 1. 在模板中使用 `v-for` 渲染出多个可拖拽的方块(`<div class="box">`)和一个 SVG 容器(`<svg class="line-container">`)来绘制连线; 2. 通过 `@mousedown` 监听鼠标按下事件,记录当前拖拽的方块的索引和起始位置; 3. 通过 `@mousemove` 监听鼠标移动事件,根据当前鼠标位置计算出方块的新位置; 4. 通过 `@mouseup` 监听鼠标抬起事件,清空当前拖拽状态; 5. 通过方法 `connectBoxes` 将最后两个方块连线。 需要注意的是,代码中通过 `reactive` 创建了一个响应式对象 `state`,并使用 `toRefs` 将其转换为响应式引用,以便在模板中使用。并且,在组件的 `mounted` 和 `beforeUnmount` 钩子中分别添加和移除了全局的鼠标移动和抬起事件监听器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃葡萄不吐葡萄皮嘻嘻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值