SVG图标使用-vue

6/6/2021 svg

# 在vue项目中使用.svg图标,使用iconfont图标库

# 封装svg

  1. 创建SvgIcon组件

SvgIcon下创建index.vue 文件

// index.vue
<template>
  <svg :class="svgClass" aria-hidden="true">
    <use :xlink:href="iconName"></use>
  </svg>
</template>

<script>
export default {
  name: 'svg-icon',
  props: {
    iconClass: {
      type: String,
      required: true
    },
    className: {
      type: String
    }
  },
  computed: {
    iconName() {
      return `#icon-${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>
  1. src跟目录下创建icons文件夹,里面创建svg文件夹和index.js文件
    • svg文件夹中用来存放各种扩展的.svg图标。
    • index.js种写入以下代码:
// index.js
import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon'// svg组件

// register globally
Vue.component('svg-icon', SvgIcon)

const requireAll = requireContext => requireContext.keys().map(requireContext)
const req = require.context('./svg', false, /\.svg$/)
requireAll(req)
  1. main.js中引入
import './icons'
  1. 下载插件
npm i svg-sprite-loader --save
  1. 配置 在build/webpack.base.conf.js文件中,加入
{
        test: /\.svg$/,
        loader: 'svg-sprite-loader',
        include: [resolve('src/icons')],
        options: {
          symbolId: 'icon-[name]'
        }
      }

并在以下设置中添加exclude: [resolve(‘src/icons’)],如下所示

  {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url-loader',
        exclude: [resolve('src/icons')],
        options: {
          limit: 10000,
          name: utils.assetsPath('img/[name].[hash:7].[ext]')
        }
      },

* 注意里面的icons文件夹的路径,路径错误,图标会显示不出来的。 6. 在项目中使用

<svg-icon icon-class="user" />
最后提交: 7/15/2022, 10:42:12 AM