Vue3.2 +Vite添加Svg的使用

Vue3.2 +Vite添加Svg的使用

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

yarn add vite-plugin-svg-icons -D 目前使用的版本是1.1.0

2. 配置vite.config.ts

  1. iconDirs所有的 svg的文件都存放在该文件夹下
  2. symbolId指定use标签中href格式
import { defineConfig } from 'vite'
import { resolve } from 'path'
import vitePluginSvgIcons from 'vite-plugin-svg-icons'

export default defineConfig(()=>{
	return {
		plugins: [
	      // 引入 svg
	      vitePluginSvgIcons({
	        iconDirs: [resolve(__dirname, 'src/assets/icon/svg')], // 所有的 svg的文件都存放在该文件夹下
	        symbolId: 'icon-[name]'
	      }),
    	],
	}
})

在这里插入图片描述

3. 封装使用全局组件

  1. html部分
    <template>
      <svg aria-hidden="true">
        <use :href="symbolId" />
      </svg>
    </template>
    
  2. js部分
    <script setup lang="ts">
    import { computed } from 'vue'
    interface IProps {
      /** svg 的图标的名称 */
      iconName: string
    }
    const props = defineProps<IProps>()
    const symbolId = computed(()=> `#icon-${props.iconName}`)
    </script>
    
  3. 注册全局组件
    import { App } from 'vue'
    app.component('SvgIcon', SvgIcon)
    
  4. 使用,这里的icon-name中必须要和 assets/icon/svg中的svg文件名称保持一致
    <SvgIcon icon-name="wechat" />
    
  5. 效果图
    在这里插入图片描述

4. 如果使用的vite-plugin-svg-icons2.x的版本, 需要修改

  1. 首先是vite.config.ts 需要导入的地方修改未为import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import { defineConfig } from 'vite'
import { resolve } from 'path'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

export default defineConfig(()=>{
	return {
		plugins: [
	      // 引入 svg
	      createSvgIconsPlugin({
	        iconDirs: [resolve(process.cwd(), 'src/assets/icon/svg')],
	        symbolId: 'icon-[name]'
	      }),
    	]
	}
})
  1. 还需要在main.ts中导入
import 'virtual:svg-icons-register'
以下是一个基于 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值