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

【教程】Vue2中使用svg矢量图

1.npm导包

npm i svg-sprite-loader --save

2.创建目录放入svg文件,创建SvgIcon.js

在这里插入图片描述

3.SvgIcon.js

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

4.vue.config.js文件中配置

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

5.在src/compoments/SvgIcon下创建SvgIcon.vue

<template><svg :class="svgClass" aria-hidden="true"><use :xlink:href="iconName"/></svg>
</template><script>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>

6.main.js

import './icons/SvgIcon.js' //vue中使用svg
import SvgIcon from './components/SvgIcon/SvgIcon.vue'//vue中使用svg
Vue.component('svg-icon', SvgIcon)//vue中使用svg

7.使用方法

<template><div><svg-icon class-name="xxxx" icon-class="xxxx"/></div>
</template><script>export default {name: 'test',
}
</script><style>.star-icon {font-size: 30px;color: gold;}
</style>

8.红框里的就是svg-icon效果

在这里插入图片描述

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

相关文章:

  • 简约唯美的404HTML源码
  • PDF 转图片并插入到 EXCEL 再转PDF
  • jmeter之变量随机参数化以及解决多线程不会随机变化
  • 24/7/12总结
  • sentinel网关限流配置及使用
  • # 如何解决 App Store 审核中的 4.3(a) 问题:Guideline 4.3(a) - Design - Spam
  • 最长上升子序列(LIS)
  • 自动驾驶车道线检测系列—3D-LaneNet: End-to-End 3D Multiple Lane Detection
  • 手工创建 postgres kamailio 数据库
  • 装饰设计模式
  • Linux 线程初步解析
  • 为ppt中的文字配色
  • python-区间内的真素数(赛氪OJ)
  • TCP/IP、UDP、HTTP 协议介绍比较和总结
  • Unity Meta Quest 开发:如何在每只手指上添加 Poke 交互
  • MyBatis的原理?
  • 数学基础【俗说矩阵】:齐次线性方程和非齐次线性方程求解-学习笔记
  • 乐尚代驾项目概述
  • 脱发的 7 个原因,不能再瞒着大家了!
  • Vim使用教程
  • 前端开发体系+html文件详解
  • 小程序中用于跳转页面的5个api是什么和区别
  • 翁恺-C语言程序设计-10-0. 说反话
  • langchain 入门指南(二)- 如何跟大模型对话
  • [集成学习]基于python的Stacking分类模型的客户购买意愿分类预测
  • FastApi地理坐标数据存取实践
  • Docker容器——初识Docker,安装以及了解操作命令
  • JavaSE从零开始到精通
  • 求解答word图标变白
  • Jenkins 离线升级