Vue3+vite动态使用SVG(支持渐变)

本文介绍了如何在低代码平台开发中,通过安装和配置vite-plugin-svg-icons插件,实现SVG图标颜色、大小的动态调整,并允许用户自定义上传SVG图标。详细步骤包括插件安装、配置、SVG组件创建、全局注册以及在应用中使用。通过这种方法,用户可以灵活地定制SVG图标以匹配不同场景的需求。
摘要由CSDN通过智能技术生成

在开发低代码平台,写数字矩形图组件时,提供配置不同的前缀图标,平台提供了多种SVG供用户选择,也可以由用户上传,针对与平台提供的SVG需要修改颜色大小。

1.安装vite-plugin-svg-icons

 npm i 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({
        <!-- svg所在文件夹位置 -->
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icon/svg')],
        symbolId: 'icon-[dir]-[name]',
      }),
    ],
  }
}

3.创建SVG组件

<template>
  <svg aria-hidden="true" class="svg-icon" :width="props.size" :height="props.size">
    <defs>
      <linearGradient id="gradient" v-bind="direction">
        <stop offset="0%" :stop-color="props.color" />
        <stop offset="100%" :stop-color="props.gradientColor" />
      </linearGradient>
    </defs>
    <use :xlink:href="symbolId" :fill="svgFill" />
  </svg>
</template>

<script setup>
import { computed } from 'vue';

const props = defineProps({
  prefix: {
    type: String,
    default: 'icon'
  },
  name: {
    type: String,
    required: true
  },
  color: {
    type: String,
    default: '#333'
  },
  size: {
    type: String,
    default: '1em'
  },
  gradientColor: {
    type: String,
    default: ''
  },
  gradientAngle: {
    type: String,
    default: '1'
  }
});

const symbolId = computed(() => `#${props.prefix}-${props.name}`);
const svgFill = computed(() => {
  if (props.gradientColor) {
    return 'url(#gradient)';
  }
  return props.color;
});
const direction = computed(() => {
  switch (props.gradientAngle) {
    case '1':
      return { x1: '0%', x2: '100%', y1: '0%', y2: '0%' };
    case '2':
      return { x1: '0%', x2: '100%', y1: '100%', y2: '0%' };
    case '3':
      return { x1: '0%', x2: '0%', y1: '100%', y2: '0%' };
    case '4':
      return { x1: '100%', x2: '0%', y1: '100%', y2: '0%' };
    case '5':
      return { x1: '100%', x2: '0%', y1: '0%', y2: '0%' };
    case '6':
      return { x1: '0%', x2: '100%', y1: '0%', y2: '100%' };
    case '7':
      return { x1: '0%', x2: '0%', y1: '0%', y2: '100%' };
    case '8':
      return { x1: '100%', x2: '0%', y1: '0%', y2: '100%' };
    default:
      return { x1: '0%', x2: '100%', y1: '0%', y2: '0%' };
  }
});
</script>

4.在src/main.js全局注册组件并内引入注册脚本

import { createApp } from 'vue'
import App from './App.vue'
import svgIcon from "@/components/SvgIcon/index.vue";
import 'virtual:svg-icons-register'
const app = createApp(App)
app.component('svg-icon', svgIcon).mount('#app')

5.使用

<svg-icon v-bind=“svgConfig” />

实例效果图

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值