首页 > 其他分享 >vue项目引入自定义svg

vue项目引入自定义svg

时间:2024-03-07 16:46:57浏览次数:19  
标签:vue 自定义 svg loader Vue SVG 组件 icon

图标可以使用element-ui的图标库、第三方的图标库或者引入svg使用,这里是讲如何使用自定义的svg。

  1. 将SVG图标放入项目

 自定义的svg可以访问 https://www.iconfont.cn地址,搜索你想要的图标,下载 SVG 格式,放入项目的src/assets/icons/svg文件夹中。并在src/assets/icons/index.js中写上代码

import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon'// svg component
Vue.component('svg-icon', SvgIcon)

const req = require.context('./svg', false, /\.svg$/)
const requireAll = requireContext => requireContext.keys().map(requireContext)
requireAll(req)

      2. 将SVG Sprite转换为Vue组件

你可以创建一个Vue组件来封装SVG Sprite,并在该组件中使用<symbol>id来引用图标。这样,你就可以像使用其他Vue组件一样使用它。

在src/components/SvgIcon/index.vue中写入代码,如下:
<template>
  <div v-if="isExternal" :style="styleExternalIcon" class="svg-external-icon svg-icon" v-on="$listeners" />
  <svg v-else :class="svgClass" aria-hidden="true" v-on="$listeners">
    <use :xlink:href="iconName" />
  </svg>
</template>

<script>
export default {
  name: 'SvgIcon',
  props: {
    iconClass: {
      type: String,
      required: true
    },
    className: {
      type: String,
      default: ''
    }
  },
  computed: {
    isExternal() {
      return /^(https?:|mailto:|tel:)/.test(this.iconClass);
    },
    iconName() {
      return `#icon-${this.iconClass}`;
    },
    svgClass() {
      if (this.className) {
        return 'svg-icon ' + this.className;
      } else {
        return 'svg-icon';
      }
    },
    styleExternalIcon() {
      return {
        mask: `url(${this.iconClass}) no-repeat 50% 50%`,
        '-webkit-mask': `url(${this.iconClass}) no-repeat 50% 50%`
      };
    }
  }
};
</script>

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

.svg-external-icon {
  background-color: currentColor;
  mask-size: cover!important;
  display: inline-block;
}
</style>

然后,在你的主入口文件main.js中全局注册这个组件,或者在需要的组件中局部注册。

import '@/assets/icons';

       3. 使用Vue SVG Sprite loader

你可以使用svg-sprite-loader来自动将SVG文件转换为Vue组件。这样,你就可以像导入其他Vue组件一样导入SVG图标,并在模板中直接使用它们。

首先,安装svg-sprite-loader

npm install svg-sprite-loader

然后,在vue.config.js中配置Webpack:

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

 

module.exports = {  
  chainWebpack(config) {
    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/assets/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/assets/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
  }
}

现在,你可以直接在你的Vue组件中导入SVG图标,并像使用其他组件一样使用它。

<svg-icon icon-class="svg图标名" class-name='样式名' />

  或者

<el-button>
    <svg-icon icon-class="svg图标名" class-name='样式名' /> 新增
</el-button>

  完成上述操作之后,重启前端项目(我就是没重启,一直不显示出来)并刷新页面,这样就能看到图标了。

 

标签:vue,自定义,svg,loader,Vue,SVG,组件,icon
From: https://www.cnblogs.com/jishugaochao/p/18059127

相关文章

  • 若依集成CIM(即时推送系统)实现将服务端修改为SpringBoot+Vue前后端分离版(文末见代码
    ​ 场景若依前后端分离版本地搭建开发环境并运行项目的教程:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108465662 CIMGitee地址:https://gitee.com/farsunset/cimCIM项目是基于mina或者netty框架下的推送系统,我们平常使用第三方的推送SDK,如极光推送,百度......
  • Vue学习笔记38--单文件组件
    单文件组件命名规则如下所示:------单个单词命名规则:------方式一:temp.vue方式二:Temp.vue建议使用(可和vue开发者工具呼应)------多个单词命名规则------方式一:my-temp.vue方式二:MyTemp.vue建议使用(可和vue开发者工具呼应)组件交互相关的代码暴露方式:1.分别暴露:exportconst......
  • 向TreeView添加自定义信息
    可在Windows窗体TreeView控件中创建派生节点,或在ListView控件中创建派生项。通过派生可添加任何所需字段,以及添加处理这些字段的自定义方法和构造函数。此功能的用途之一是将Customer对象附加到每个树节点或列表项。虽然此处的示例是关于TreeView控件的,但该方法同样......
  • Vue+Axios的方法异步回调顺序问题
    一、问题阐述有的时候我们需要控制异步函数的执行顺序,比如a方法中如果要用到异步函数b方法的请求结果,就需要进行顺序控制,否则a函数先执行就会导致找不到数据直接报错。二、方法1.异步控制1.1.async,await等做异步控制1.2修改函数放置位置达到异步控制效果(我遇到的情况无效,但......
  • vue_居中左对齐
    div中文本居中对齐后,然后再左对齐如下效果:===============================22345645=================2345678987654============12========================================= <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8......
  • Mysql和Oracle自定义函数区别
    1.Mysql自定义函数dropfunctionifexistsget_date;createfunctionget_date(v_datetimedatetime,v_timevarchar(50))returnsvarchar(50)begin declarev_datevarchar(50); declarecDatevarchar(50); declarebeginDatevarchar(50); setcDate=date_format(......
  • Vue调试神器vue-devtools配置 / 解决提示 Download the Vue Devtools extension for a
    访问Vue页面,控制台提示:    ......
  • vue2项目中不能直接在store中声明响应式变量,vue3项目中能在store中直接声明响应式变量
    vue2项目中不能直接在store中声明响应式变量,vue3项目中能在store中直接声明响应式变量,页面元素也会响应式生效在Vue2项目中,store中的状态默认情况下是不具备响应式的特性的。这是因为Vue2.x使用的是基于对象定义的Vue.observable()来创建响应式对象,而store中的状态是通......
  • Mysql自定义函数报错合集
    参考:Mysql自定义函数报错解决方法1.在MySql中创建自定义函数报错信息如下:1.1错误显示ERROR1418(HY000):ThisfunctionhasnoneofDETERMINISTIC,NOSQL,orREADSSQLDATAinitsdeclarationandbinaryloggingisenabled(youmightwanttousethelesssafel......
  • Vue学习笔记37--内置关系
    示例一:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>一个重要的内置关系</title>......