当前位置: 首页 > news >正文

封装svg图片

前言

项目中有大量svg图片,为了方便引入,所以对svg进行了处理

一、svg是什么?

svg是可缩放矢量图形,是一种图片格式

二、使用步骤

1.创建icons文件夹

将icons文件夹放进src中,并创建一个svg文件夹和index.js,SvgIcon.vue以及svgo.yml

2.封装svg-icon(SvgIcon.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>// doc: https://panjiachen.github.io/vue-element-admin-site/feature/component/svg-icon.html#usageimport { isExternal } from '@/utils/validate'export default {name: 'SvgIcon',props: {iconClass: {type: String,required: true},className: {type: String,default: ''}},computed: {isExternal() {return isExternal(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>

3.校验传入的iconClass是否为外部链接 

(若src文件夹下有utils则直接创建validate.js,没有则创建utils文件夹和validate.js)

/**校验传入的iconClass是否为外部链接* @param {string} path* @returns {Boolean}*/export function isExternal(path) {return /^(https?:|mailto:|tel:)/.test(path)}

4.全局安装下载(index.js)

import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon'// svg component// register globally
Vue.component("svg-icon", SvgIcon);const req = require.context('./svg', false, /\.svg$/)
const requireAll = requireContext =>{requireContext.keys().map(requireContext)
} 
requireAll(req)

svgo.yml

# replace default config# multipass: true
# full: trueplugins:# - name## or:# - name: false# - name: true## or:# - name:#     param1: 1#     param2: 2- removeAttrs:attrs:- 'fill'- 'fill-rule'

main.js

import '@/icons/index'

 5.下载插件批量处理svg图片

npm install svg-sprite-loader
npm install svgo-loader

我下载的版本是

6.vue.config.js配置插件 

const path = require('path')
module.exports = {chainWebpack: (config) => {let args = ''config.resolve.alias.set('@', resolve('src')).set('@assets', resolve('src/assets')).set('@views', resolve('src/views')).set('@components', resolve('src/components')).set('@images', resolve('src/assets/images')).set('@utils', resolve('src/utils'))config.module.rule('svg').exclude.add(path.join(__dirname, 'src/icons/svg')).end()config.module.rule('icons').test(/\.svg$/).include.add(path.join(__dirname, 'src/icons/svg')).end().use('svg-sprite').loader('svg-sprite-loader').options({symbolId: 'icon-[name]',svgo: {plugins: [{removeViewBox: false}]}}).end().use('svgo-loader').loader('svgo-loader').options({plugins: [{name: 'removeAttrs', // 必须指定name!params: {attrs: 'fill'}}]}).end()}
}

 7.使用svg

(将svg图片引入svg文件夹中并修改名字,然后使用以下代码方可使用)

//home是svg图片在svg文件夹中的名字,color是svg图片的颜色<svg-icon icon-class="home" color="#fff" />

总结

  以上内容皆是我总结网上查询到的以及根据自身项目所得出的经验,可供参考。

http://www.lryc.cn/news/443599.html

相关文章:

  • tomcat的Catalinalog和localhostlog乱码
  • 行人持刀检测数据集 voc yolo
  • 基于51单片机的汽车倒车防撞报警器系统
  • NLP 文本匹配任务核心梳理
  • FastAPI 的隐藏宝石:自动生成 TypeScript 客户端
  • 了解云容器实例云容器实例(Cloud Container Instance)
  • OpenStack Yoga版安装笔记(十三)neutron安装
  • [系列]参数估计与贝叶斯推断
  • 【Pyside】pycharm2024配置conda虚拟环境
  • 【RabbitMQ 项目】服务端:数据管理模块之消息队列管理
  • SDKMAN!软件开发工具包管理器
  • 《使用 LangChain 进行大模型应用开发》学习笔记(四)
  • gbase8s数据库常见的索引扫描方式
  • 边缘智能-大模型架构初探
  • 《python语言程序设计》2018版第8章18题几何circle2D类(上部)
  • nginx upstream转发连接错误情况研究
  • alias 后门从入门到应急响应
  • 【远程调用PythonAPI-flask】
  • [今日Arxiv] 思维迭代:利用内心对话进行自主大型语言模型推理
  • glTF格式:WebGL应用的3D资产优化解决方案
  • Unity3D入门(一) : 第一个Unity3D项目,实现矩形自动旋转,并导出到Android运行
  • 数据结构与算法——Java实现 8.习题——移除链表元素(值)
  • 如何理解MVCC
  • 在 Qt 中使用 QLabel 设置 GIF 动态背景
  • Flyway 数据库差异处理
  • CSS 选择器的分类与使用要点一
  • 无人机集群路径规划:麻雀搜索算法(Sparrow Search Algorithm, SSA)​求解无人机集群路径规划,提供MATLAB代码
  • harbor集成trivy镜像扫描工具
  • DMA学习
  • C语言18--头文件