首页 > 其他分享 >在Vue2项目(基于Vue Cli)中封装一个便捷的svg图标(SvgIcon)组件

在Vue2项目(基于Vue Cli)中封装一个便捷的svg图标(SvgIcon)组件

时间:2022-11-25 23:33:42浏览次数:44  
标签:vue Cli svg loader Vue SvgIcon icon

主要是介绍下SVG图标组件在Vue2.x中的配置流程

首先,假设你有一个图标为:calculator.svg

最后想要通过下图的这种方式来使用它:

 

遵循以下步骤便可以实现:

1. 先编写一个SvgIcon组件

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

<script>
export default {
  name: 'SvgIcon',
  props: {
    icon: {
      type: String,
      required: true,
    },
  },
  computed: {
    iconName() {
      return '#icon-' + this.icon;
    },
  },
};
</script>

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

2. 添加一个icon-requirer.js来完成本地svg图标集的引入

const svgResources = require.context('./assets/svg/icons', false, /\.svg$/);
svgResources.keys().forEach(svg => {
  svgResources(svg);
});

3. 将icon-requirer.js引入main.js,并全局注册SvgIcon组件

import Vue from 'vue';
import App from './App.vue';
import SvgIcon from './components/SvgIcon.vue';
import './icon-requirer';

Vue.component(SvgIcon.name, SvgIcon);
Vue.config.productionTip = false;

new Vue({
  render: h => h(App),
}).$mount('#app');

4. 下载 svg-sprite-loader 和 svgo.loader 两个webpack loader

前者是一个用于创建 svg 雪碧图的 Webpack 加载器,它会把你引入的 svg 塞到一个个 symbol 中,合成一个大的 svg 插入到文档内;

后者可以帮助我们手动删除svg文件代码中的fill属性;

npm i -D svg-sprite-loader svgo-loader

5. 更改vue.config.js中的配置

const path = require('path')
const { defineConfig } = require('@vue/cli-service')

const resolve = dir => path.join(__dirname, dir);

module.exports = defineConfig({
  transpileDependencies: true,
  chainWebpack(config) {
    config.module.rule('svg')
      .exclude
      .add(resolve('./src/assets/svg/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('./src/assets/svg/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]',
      })
      .end()
      .use('svgo-loader')
      .loader('svgo-loader')
      .options({
        plugins: [
          {
            name: 'removeAttrs',
            params: {
              attrs: 'fill',
            },
          }
        ],
      })
      .end();
  }
});

如上配置后,就可以了

最后,贴出package.json的依赖内容(Vue Cli是5.x的版本)

"dependencies": {
  "core-js": "^3.8.3",
  "vue": "^2.6.14"
},
"devDependencies": {
  "@babel/core": "^7.12.16",
  "@babel/eslint-parser": "^7.12.16",
  "@vue/cli-plugin-babel": "~5.0.0",
  "@vue/cli-plugin-eslint": "~5.0.0",
  "@vue/cli-service": "~5.0.0",
  "eslint": "^7.32.0",
  "eslint-plugin-vue": "^8.0.3",
  "svg-sprite-loader": "^6.0.11",
  "svgo-loader": "^3.0.3",
  "vue-template-compiler": "^2.6.14"
},

 

标签:vue,Cli,svg,loader,Vue,SvgIcon,icon
From: https://www.cnblogs.com/fanqshun/p/16926663.html

相关文章

  • vue跳转页面常用的几种方法
    vue跳转页面有好几种不同方法,下面将通过实例代码给大家介绍,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下。1:router-link跳转2:this.$router.push()3......
  • vuex-getters
    有时候,需要从store中获取一些state变化之后的数据,这是可以放入getters中     ......
  • 再次详解clientHeight、offsetHeight、scrollHeight
    关于clientHeight、offsetHeight、scrollHeightwindow.screen.availWidth返回当前屏幕宽度(空白空间)window.screen.availHeight返回当前屏幕高度(空白空间)window.s......
  • VUE框架(案例_数据可视化)
    使用vue进行数据可视化常用的插件场景插件简介文档数据分析展示v-charts https://vue-echarts.github.io/guide/ highcharts https://www.highchart......
  • Vue3
    helloworld<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewpo......
  • 2.4 ap client 中继 能连接5g 路由信号吗?
      2.4apclient中继能连接5g路由信号吗?2.4apclient中继能连接5g路由信号吗?2.4apclient中继能连接5g路由信号吗?......
  • vuex-mutations
    通过mutations修改state状态this.$store.commit('mutations中的方法')  conststore=newVuex.Store({state:{counter:100},mutati......
  • vuex页面刷新数据丢失问题的四种解决方式
    vuex是大家使用vue时大多数都会选择的,但是当页面刷新之后vuex数据会丢失,下面这篇文章主要给大家介绍了关于vuex页面刷新数据丢失问题的四种解决方式,需要的朋友可以参考......
  • vue2 生命周期14 生命周期 数据共享 ref引用
    生命周期:生命周期(LifeCycle)是指一个组件从创建->运行->销毁的整个阶段,强调的是一个时间段 创建阶段:beforeCreate:创建之前created:创建beforeM......
  • 2g信号出不来 2g的ap client肯定就用不了 是吧
      2g信号出不来2g的apclient肯定就用不了是吧 2g信号出不来2g的apclient肯定就用不了是吧2g信号出不来2g的apclient肯定就用不了是吧......