avatar

mdo

Hello

  • 首页
  • 知识库
  • 归档
  • 标签
  • 关于
主页 Vue中svg图片引入的两种方式
文章

Vue中svg图片引入的两种方式

发表于 2025-09-13 更新于 2025-09- 13
作者 mdo
15~19 分钟 阅读

1、vue文件中写入svg方法

A. 创建图标目录


B. IconBase.vue内容如下

<template>
  <svg
    xmlns="http://www.w3.org/2000/svg"
    :width="width"
    :height="height"
    viewBox="0 0 18 18"
    :aria-labelledby="iconName"
    role="presentation"
  >
    <title :id="iconName" lang="en">{{ iconName }} icon</title>
    <g :fill="iconColor">
      <slot />
    </g>
  </svg>
</template>

<script>
export default {
  props: {
    iconName: {
      type: String,
      default: "heart",
    },
    width: {
      type: [Number, String],
      default: 18,
    },
    height: {
      type: [Number, String],
      default: 18,
    },
    iconColor: {
      type: String,
      default: "currentColor",
    },
  },
};
</script>

<style scoped>
svg {
  display: inline-block;
  vertical-align: baseline;
  margin-bottom: -2px; 
}
</style>

C. 以IcoonHeart.vue为例,内容如下:

<template>
  <path d="M11.8 1c-1.682 0-3.129 1.368-3.799 2.797-0.671-1.429-2.118-2.797-3.8-2.797-2.318 0-4.2 1.882-4.2 4.2 0 4.716 4.758 5.953 8 10.616 3.065-4.634 8-6.050 8-10.616 0-2.319-1.882-4.2-4.2-4.2z"></path>
</template>

D. js中引入Icon组件

import IconBase from '../../components/IconBase.vue';
import IconHeart from '../../components/icons/IconHeart.vue';
new Vue({
  components: {
    IconBase,
    IconHeart,
  },
})

E. 页面template中使用

<icon-base width="24" height="24" icon-color="#E15E7D" v-if="iconType === 1">
  <icon-heart />
</icon-base>

2、 webpack中配置svg格式的打包支持

A. 安装svg-sprite-loader

npm i svg-sprite-loader -D

B. webpack配置中增加对svg格式的编译支持

支持特定目录下svg文件的解析编译

{
  test: /.svg$/,
  loader: "svg-sprite-loader",
  include: [path.resolve(__dirname, '../src/icons/svg')],
},

之前配置的字体svg文件解析中排除特定目录下的svg文件,避免编译时应用到该规则

{
  test: /.(ttf|woff|eot|svg|woff2)(?[sS]+)?$/,
  exclude: [
    path.resolve(__dirname, '../src/icons/svg'), //排除字体图标文件
  ],
  use: [
    process.env.NODE_ENV === 'production'
      ? `url-loader?limit=20000&name=${path.posix.join(config.build.assetsSubDirectory, 'plugin/font/[name].[ext]?[hash]')}`
      : `url-loader?limit=20000&name=${path.posix.join(config.dev.assetsSubDirectory, 'plugin/font/[name].[ext]?[hash]')}`
  ]
}

C. svg格式文件的存放目录


D. svg展示组件SvgIcon

组件内容:

<template>
    <svg :class="svgClass" aria-hidden="true" @click="$emit('click')">
        <use :xlink:href="iconName"></use>
    </svg>
</template>

<script>

 * svg 图标组件
 * iconClass="图标名称"
 * className="风格名称"
 */
export default {
  name: 'svg-icon',
  props: {
    iconClass: { type: String, required: true },
    className: { type: String }
  },
  computed: {
    iconName () {
      return `#${this.iconClass}`
    },
    svgClass () {
      if (this.className) {
        return 'svg-icon ' + this.className
      } else {
        return 'svg-icon'
      }
    }
  }
}
</script>

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

E.注册SvgIcon component, 并引入所有需要的svg文件

import Vue from 'vue'
import SvgIcon from './SvgIcon.vue';


Vue.component('svg-icon', SvgIcon)
const requireAll = requireContext => {







  requireContext.keys().map(requireContext);
}
const req = require.context('../icons/svg', false, /.*.svg$/)
requireAll(req)

F.入口文件中引入

import '../../components/index_svg.js';

G.页面中使用

  <svg-icon icon-class="cry"/>

两种方式优缺点

第一种方式不用安装svg-sprite-loader,也无需配置webpack中针对svg格式文件的打包规则,这块比较方便;但缺点是每次增加svg格式文件都需要在component/icons下增加一个对应vue文件,并且需要同步更新主文件中相对该文件的引入,使用起来不是很方便。

第二种方式安装配置这块稍微需要点时间,但整体来说使用方便,个人比较推荐~

技术
许可协议:  CC BY 4.0
分享

相关文章

10月 2, 2026

iOS 侧载(Sideloading)工具

AltStore 和 Sideloadly 都是目前最流行的 iOS 侧载(Sideloading)工具。 简单来说,它们的作用就是让你绕过苹果的官方 App Store,直接把第三方的 IPA 安装包(也就是 iOS 的应用安装文件)安装到你的 iPhone 或 iPad 上。 通过这两个工具,你

9月 28, 2026

注册 Chat Participant

将当前服务接入 VS Code 的 AI 聊天窗口是完全可行的。VS Code 在近期的版本中正式推出了 Chat Extensions API(github.copilot.chat),允许第三方扩展将自定义的 LLM 服务或工具注册到 Copilot Chat 面板中。 结合当前 code_ge

9月 23, 2026

php intelephense 设置 if 空格

在 VS Code 中,如果你使用的是 PHP Intelephense 插件,控制 if 关键字后面是否加空格的设置是由其内置的格式化引擎(基于 PSR-12 标准)决定的。 你可以通过修改 VS Code 的 settings.json 来配置这个行为: 1. 开启 if 后面的空格(默认/推荐

下一篇

免费开源 DDNS 动态域名解析工具

上一篇

vite打包时发布时,放在服务器的二级目录中

最近更新

  • 比尔盖茨的智慧:想多赚钱,就每天循环做这3件事
  • iOS 侧载(Sideloading)工具
  • 程序员越想创业,越不要急着动手
  • 注册 Chat Participant
  • 中秋给在外游子的一封信

热门标签

API CodeGeex Coding Cursor DeepSeek Docker Gitkraken Harness Laravel Management

目录

©2026 mdo. 保留部分权利。