美文网首页
在Vue中使用svg

在Vue中使用svg

作者: 取个帅气的名字真好 | 来源:发表于2019-05-10 20:50 被阅读0次

1、安装svg-sprite-loader

npm install svg-sprite-loader --save-dev

2、/src/components 创建 SvgIcon.vue

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

<script>
/**
 * 使用栗子
 *   <svg-icon icon-class="set"></svg-icon>
 */
  export default {
    name: 'SvgIcon',
    props: {
      iconClass: {
        type: String,
        required: true
      },
      className: {
        type: String,
        default: ''
      }
    },
    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>
目录结构

3、src/icons/index.js

import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon' // svg组件
// 注册全局组件
Vue.component('svg-icon', SvgIcon)
const requireAll = reqireContext => reqireContext.keys().map(reqireContext)
const req = require.context('./svg', false, /\.svg$/)
requireAll(req)

4、src/icons/svg 放svg即可(最好使用iconfont上的svg)

5、vue-config.js配置

const path = require('path')
function resolve (dir) {
  return path.join(__dirname, './', dir)
}

module.exports = {
  configureWebpack: config => {
},
  chainWebpack: config => {
    // svg loader
    const svgRule = config.module.rule('svg') // 找到svg-loader
    svgRule.uses.clear() // 清除已有的loader, 如果不这样做会添加在此loader之后
    svgRule.exclude.add(/node_modules/) // 正则匹配排除node_modules目录
    svgRule // 添加svg新的loader处理
      .test(/\.svg$/)
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })

    // 修改images loader 添加svg处理
    const imagesRule = config.module.rule('images')
    imagesRule.exclude.add(resolve('src/icons'))
    config.module
      .rule('images')
      .test(/\.(png|jpe?g|gif|svg)(\?.*)?$/)
  },
}
}

6、使用
index.vue

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

ps: 修改svg颜色可使用color:red;或者fill:red;

本文参考花裤衩

相关文章

  • 插件工具 | 在vue中引入svg图标

    前言 1、基于 vue-cli32、使用插件 svg-sprite-loader 在 src/icons/svg文...

  • 项目使用svg图标

    vue-element-admin基础模板中,已经封装好了使用svg图标的组件,此组件在vue-ssr项目中也同样...

  • vue中使用svg注意点

    网上介绍在vue中如何使用svg的教程已经很多了,我这不多做赘述。 场景 已经根据网上的教程,将svg组件引入到页...

  • vue-cli3.x使用svg可复用图标

    vue-cli3.x使用svg可复用图标 在使用svg绘制时,对于可复用的图元,我们一般会使用标签 ,然后将每个图...

  • 在Vue中使用svg

    1、安装svg-sprite-loader。 2、/src/components 创建 SvgIcon.vue 3...

  • vue使用svg

    vue使用svg 做的一个可视化大屏项目中需要引入svg, 直接绑定svg元素的某些值,在ui给的svg中出现了s...

  • Vue中使用SVG封装成SVG组件

    一、在vue脚手架生成的文件夹下的src/components创建一个Svg 二、使用和安装svg-sprite ...

  • vue 使用 svg

    说明:很多关于类似的博客或者文档介绍的第三方库,由于有文件目录配置单一或者有webpack 兼容问题,所以都不考虑...

  • vue使用svg

    友情链接:https://www.jianshu.com/p/15f9e3a5e00d 终端安装 npm npm ...

  • vue使用svg

    在vue中使用svg svg的好处矢量性(无论图片放多大,都不会出现锯齿状模糊)利于seo 1. 安装依赖 2. ...

网友评论

      本文标题:在Vue中使用svg

      本文链接:https://www.haomeiwen.com/subject/waszoqtx.html