vue3+js+vite学习之使用svg图标,并且支持svg图标渐变色

使用vite-plugin-svg-icons

1、安装vite-plugin-svg-icons

npm install vite-plugin-svg-icons
或者
yarn add vite-plugin-svg-icons

2、在vite.config.js中进行配置

import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

export default ({ mode }) => {
  const base = '/'
  return defineConfig({
    base,
    plugins: [
      vue(),
      createSvgIconsPlugin({
        // 指定要缓存的图标文件夹(将所有svg都放在一个文件夹下)
        iconDirs: [path.resolve(process.cwd(), 'public/icons')],
        // 执行icon name的格式(dir代表文件夹名称,name代表icon名称)
        symbolId: 'icon-[dir]-[name]'
      })
    ]
  })
}
 

3、组件封装(支持渐变色)

在src/components文件夹下新建SvgIcon文件夹,然后在SvgIcon文件夹下新建index.vue文件

<template>
  <svg :class="svgClass" aria-hidden="true">
    <defs>
      <linearGradient :id="`gradient-${randomNum}`" v-bind="direction">
        <stop :stop-color="props.color" />
        <stop offset="1" :stop-color="props.gradientColor" />
      </linearGradient>
    </defs>
    <use :xlink:href="iconName" :fill="svgFill" />
  </svg>
</template>
<script setup>
import { computed } from 'vue'
let randomNum = Math.random();

const props = defineProps({
  // 图标名称
  name: {
    type: String,
    required: true
  },
  // 单一颜色
  color: {
    type: String,
    default: ''
  },
  // 渐变色
  gradientColor: {
    type: String,
    default: ''
  },
  // 渐变色角度
  gradientAngle: {
    type: String,
    default: '1'
  }
})
// svg的外层div的class
const svgClass = computed(() => {
  if (props.name) {
    return `svg-icon icon-${props.name}`
  } else {
    return 'svg-icon'
  }
})
// svg图标的名称
const iconName = computed(() => {
  return `#icon-${props.name}`
})
// svg填充颜色,如果gradientColor值存在,就是渐变色,否则是单一颜色
const svgFill = computed(() => {
  if (props.gradientColor) {
    return `url(#gradient-${randomNum})`
  }
  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>

<style scoped>
.svg-icon {
  width: 1em;
  height: 1em;
  fill: currentColor;
  vertical-align: middle;
}
</style>

4、在main.js中注册

// 引入注册脚本
import 'virtual:svg-icons-register'
// 引入组件
import SvgIcon from '@/components/SvgIcon/index.vue'
// 全局组件挂载
app.component('SvgIcon', SvgIcon)

5、使用

<template>
  <svg-icon name="icon-home" color="#f00"/>
  <svg-icon name="icon-back" color="#f00" gradientColor="#0f0" gradientAngle="8" />
</template>

Vue3 + TypeScript + UniApp(原Dcloud)项目中,结合Vite构建工具,引入SVG图标通常分为以下几个步骤: 1. 安装依赖:首先需要安装`vue-svg-loader` 和 `@types/vue-svg-icon`,这两个库分别用于处理SVG文件并在Vue组件中使用。你可以通过npm或yarn来安装它们: ``` npm install vue-svg-loader @types/vue-svg-icon --save-dev ``` 2. 配置loader:在`.vitepress/vite.config.ts` 或 `.vite/config.js` 文件中,添加SVG加载器配置到模块解析规则中。例如: ```javascript import { defineConfig } from &#39;vite&#39;; import vueSvgLoader from &#39;vue-svg-loader&#39;; export default defineConfig({ build: { // ... 其他配置项 loaderOptions: { svg: { // 使用 vue-svg-loader 解析 SVG 文件 transform: vueSvgLoader(), }, }, }, }); ``` 3. 使用SVG:在Vue组件中,可以直接将SVG内容插入到`<template>`标签内,或者作为`<img>`元素src属性的值。如果要动态引用SVG,可以创建一个包含路径的对象,并在模板中通过`<template v-bind:src="iconPath">`引用它: ```html <template> <div :class="{&#39;my-svg&#39;: true}"> <img v-if="!isIconString" :src="iconPath" alt="SVG Icon"> <template v-else> <svg :viewBox="icon.viewbox" :width="icon.width" :height="icon.height"> <use :href="#{{ icon.id }}"></use> </svg> </template> </div> </template> <script setup lang="ts"> const icon = { id: "my-svg-icon", viewBox: "0 0 24 24", width: 24, height: 24, pathData: `<path d="M..."></path>`, // 你的SVG数据 }; const isIconString = false; // 根据需求设置为布尔值 // 在data()或setup()中获取并传递给模板 const iconPath = isIconString ? "#" + icon.id : icon.pathData; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值