vue cli3.0使用svg全过程

时间:2022-10-21 23:51:45

VUE-cli3使用 svg-sprite-loader

svg-sprite-loader 的插件,用来根据导入的 svg 文件自动生成 symbol 标签并插入 html

1.安装依赖

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

2.配置vue.config.js文件
webpack 配置,在Vue.config.js加入处理 svg 的 loader

const path = require('path')
function resolve (dir) {
return path.join(__dirname, '.', dir)
}
module.exports = {
chainWebpack: config => {
config.module.rules.delete('svg') // 重点:删除默认配置中处理svg,
// const svgRule = config.module.rule('svg')
// svgRule.uses.clear()
config.module
.rule('svg-sprite-loader')
.test(/\.svg$/)
.include
.add(resolve('src/icons')) // 处理svg目录
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'icon-[name]'
})
},
}

3.在src/components下新建SvgIcon.vue文件

<template>
<!--<svg class="svgClass" aria-hidden="true">
<use :xlink:href="iconName"></use>
</svg>-->
<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>
import { isExternal } from '@/utils/validate'
export default {
name: 'SvgIcon',
props: {
iconClass: {
type: String,
required: true
},
className: {
type: String,
default: ''
}
},
computed: {
isExternal () {
// return false
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>

4.在src下新建文件夹utils及文件validate.js
参考vue-element-admin中svg配置
panjiachen.github.io/...

/**
* Created by PanJiaChen on 16/11/18.
*/ /**
* @param {string} path
* @returns {Boolean}
*/
export function isExternal (path) {
return /^(https?:|mailto:|tel:)/.test(path)
} /**
* @param {string} str
* @returns {Boolean}
*/
export function validUsername (str) {
const validmap = ['admin', 'editor']
return validmap.indexOf(str.trim()) >= 0
}

5.在src下新建icons文件夹,及icons文件夹下新建svg文件夹、index.js文件、svgo.yml文件

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: true plugins: # - name
#
# or:
# - name: false
# - name: true
#
# or:
# - name:
# param1: 1
# param2: 2 - removeAttrs:
attrs:
- 'fill'
- 'fill-rule'

6.在main.js中引入svg

import './icons'

7.使用svg图标,将svg图标放在icons/svg文件夹下,icon-class的值就是你svg图标文件的命名

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

vue cli3.0使用svg全过程的更多相关文章

  1. vue cli3&period;0 结合echarts3&period;0和地图的使用方法

    echarts 提供了直观,交互丰富,可高度个性化定制的数据可视化图表.而vue更合适操纵数据. 最近一直忙着搬家,就没有更新博客,今天抽出空来写一篇关于vue和echarts的博客.下面是结合地图的 ...

  2. 解决vue&sol;cli3&period;0 语法验证规则 ESLint&colon; Expected indentation of 2 spaces but found 4&period; &lpar;indent&rpar;

    当你使用vue/cli3.0的时,有可能出现雁阵规则 ESLint: Expected indentation of 2 spaces but found 4. (indent) 解决方法 1.在vu ...

  3. 01-路由跳转 安装less this&period;&dollar;router&period;replace&lpar;path&rpar; 解决vue&sol;cli3&period;0语法报错问题

    2==解决vue2.0里面控制台包的一些语法错误. https://www.jianshu.com/p/5e0a1541418b 在build==>webpack.base.conf.j下注释掉 ...

  4. vue cli3&period;0打包

    1.vue cli3.0需要在项目根目录下配置webpack  包括反向代理以及打包文件路径 const webpack = require("webpack"); module. ...

  5. 使用VUE CLI3&period;0搭建项目vue2&plus;scss&plus;element简易版

    1.安装Vue CLI 3 //三选一即可cnpm install -g @vue/cli npm install -g @vue/cli yarn global add @vue/cli 注意: 1 ...

  6. vue cli3&period;0配制axios代理

    今天学习时,想访问网易新闻接口,结果显而易见,因为跨域被浏览器拒绝了. 去网上找一下结果一开始找到的是2.x版本的配置,生硬的放进去,给我各种报错.编译阶段就炸了.浪费好多时间 再按3.0版本去搜索才 ...

  7. vue cli3&period;0快速搭建项目详解(强烈推荐)

    这篇文章主要介绍下vue-cli3.0项目搭建,项目结构和配置等整理一下,分享给大家. 一.介绍 Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统.有三个组件: CLI:@vue/cl ...

  8. vue cli3&period;0 首次加载优化

    项目经理要求做首页加载优化,打包后从十几兆优化到两兆多,记下来怕下次忘记 运行report脚本 可看到都加载了那些内容,在从dist文件中index.html 查看首次加载都加载了那些东西,如下图:然 ...

  9. vue cli3&period;0 build 打包 的 js 文件添加版本号 解决 js 缓存问题

    在 vue.config.js 的文件中加入下面这段话 // vue.config.jsconst Timestamp = new Date().getTime();module.exports = ...

随机推荐

  1. BLOCK封装带菊花的网络请求

    #import <Foundation/Foundation.h> @class HttpRequestManager; typedef void(^httpRequestBlock) ( ...

  2. IOS开发UI基础 UIAlertView的属性

    UIAlertView1.Title获取或设置UIAlertView上的标题. 2.Message获取或设置UIAlertView上的消息 UIAlertView *alertView = [[UIA ...

  3. 使用面向对象思想处理cookie

    实例:使用面向对象思想处理cookie如果读者对cookie 不熟悉,可以在第七章学习它的使用方法,虽然在那里创建了几个通用函数用于cookie 的处理,但这些函数彼此分离,没有体现出是一个整体.联想 ...

  4. linux安装rz和sz

    rz命令是用来上传文件 sz命令是用来下载文件 1.系统安装了yum 以root用户登录: yum install lrzsz -y 2.没有安装yum 以下地址中有详解 http://jingyan ...

  5. 选择器,&dollar;&lpar;&quot&semi;A&plus;B&quot&semi;&rpar; 和&dollar;&lpar;&quot&semi;A~B&quot&semi;&rpar; 的理解

    在我发表这个理解之前,我有看过博客园 永恒浪子 大神的 JQuery选择器大全(http://www.cnblogs.com/hulang/archive/2011/01/12/1933771.htm ...

  6. 面试之Java知识整理

    1.面向对象都有哪些特性 继承.封装.多态性.抽象 2.Java中实现多态的机制是什么? 继承与接口 3.Java中异常分为哪些种类 3.1按照异常需要处理的时机分为编译时异常(CheckedExce ...

  7. 读书笔记 effective c&plus;&plus; Item 13 用对象来管理资源

    1.不要手动释放从函数返回的堆资源 假设你正在处理一个模拟Investment的程序库,不同的Investmetn类型从Investment基类继承而来, class Investment { ... ...

  8. 剑指Offer——知识点储备-Java基础

    剑指Offer--知识点储备-Java基础 网址来源: http://www.nowcoder.com/discuss/5949?type=0&order=0&pos=4&pa ...

  9. MYSQL—— year类型的使用与注意点!

    mysql的日期与时间类型:分为time.date.datetime.timestamp.year,主要总结下year的用法: 1.类型支持:year 与 year(4),注意无year(2)的定义方 ...

  10. 【译】如何在 Android 5&period;0 上获取 SD卡 的读写权限

    因为最近项目需要,涉及到 SD卡 的读写操作,然而申请 <!-- 读写权限 --> <uses-permission android:name="android.permi ...