在vue2中使用 SVG 图标

本文介绍了如何在 Vue2 项目中使用 SVG 图标。首先,需要安装 svg-sprite-loader 和 svgo 插件。接着,在 components 文件夹创建 svgIcon.vue 组件,并在 src/icons 目录下组织 svg 资源。通过 index.js 导出图标并在 main.js 中全局引入。最后,更新 webpack 配置以支持 SVG 图标的处理。遵循这些步骤,可以从 iconfont 等平台下载 SVG 文件并顺利在项目中使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 安装svg-sprite-loader和svgo插件

yarn add svg-sprite-loader svgo
  1. 在components文件夹下面增加svgIcon.vue组件

// svgIcon.vue
<template>
  <svg :class="svgClass" aria-hidden="true" :width="swidth" :height="sheight">
    <use :xlink:href="iconName" />
  </svg>
</template>

<script>
export default {
  name: "SvgIcon",
  props: {
    iconClass: {
      type: String,
      required: true
    },
    className: {
      type: String,
      default: ""
    },
    size: {
      type: [String, Number],
      default: 20
    }
  },
  computed: {
    iconName() {
      return `#icon-${this.iconClass}`;
  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值