首页 > 其他分享 >VUE3.0 中如何使用SVG图标

VUE3.0 中如何使用SVG图标

时间:2022-10-19 12:12:26浏览次数:67  
标签:index sprite SVG loader VUE3.0 svg icon SvgIcon 图标

1.文件下新建SvgIcon文件夹以及子文件index.js,index.vue,svg文件夹(用于存放svg图片)

 

2.编写index.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 () {
      console.log('this.className')
      console.log(this.className)
      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;
  color:#333;
  /* fill: currentColor; */
  /* background-color:#6EC1FA !important; */
  padding:0px 5px 0px 8px;
}
</style>

3.index.js

//下面这个是导入svgIcon/svg下的所有svg文件

const requireAll = requireContext => requireContext.keys().map(requireContext)

 const req = require.context('./svg', false, /\.svg$/) 

 console.log("REQ", requireAll(req))

/*

   第一个参数是:'./svg' => 需要检索的目录,

   第二个参数是:false => 是否检索子目录,

   第三个参数是: /\.svg$/ => 匹配文件的正则

  */

requireAll(req);

4.main.js中引入并全局注册

import '@/SvgIcon/index.js'

import SvgIcon from '@/SvgIcon/index.vue'// svg组件



app.component('svg-icon', SvgIcon);

5..安装svg-sprite-loader

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

6.配置vue.congfig.js

    chainWebpack: (config) => {

        //配置 svg-sprite-loader
        // 第一步:让其他svg loader不要对src/icons进行操作
        config.module
        .rule('svg')
        .exclude.add(resolve('src/SvgIcon/svg'))//注意:路径要具体到存放的svg的路径下,不然会报错
        .end() // 第二步:使用svg-sprite-loader 对 src/icons下的svg进行操作
        config.module
        .rule('icons')
        .test(/\.svg$/)
        .include.add(resolve('src/SvgIcon/svg'))//注意:路径要具体到存放的svg的路径下,不然会报错
        .end()
        .use('svg-sprite-loader')
        .loader('svg-sprite-loader')
        //定义规则 使用时 <svg class="icon"> <use xlink:href="#icon-svg文件名"></use>  </svg>
        .options({
          symbolId: 'icon-[name]'
        })
        .end()
    },

7.在页面内直接使用 svg-icon

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

d:svg文件名称

finally

转载:https://www.jianshu.com/p/e82a85e2e04d

标签:index,sprite,SVG,loader,VUE3.0,svg,icon,SvgIcon,图标
From: https://www.cnblogs.com/lst619247/p/16805822.html

相关文章

  • 2022-10-16 阿里云图标 全选 加入购物车代码
    varspan=document.querySelectorAll('.icon-cover');for(vari=0,len=span.length;i<len;i++){console.log(span[i].querySelector('span').click()......
  • JavaFx 使用字体图标记录
    原文:JavaFx使用字体图标记录-Stars-One的杂货小窝之前其实也是研究过关于字体图标的使用,还整了个库Tornadofx学习笔记(4)——IconTextFx开源库,整合5000+个字体图标......
  • WPF结合阿里巴巴矢量图标库使用ttf格式的图标字体
    一、阿里巴巴图标矢量库(https://www.iconfont.cn/)1、创建字体工程 2、往工程里添加图标 3、生成字体文件 4、window下安装字体库并查看查看对应的unicode码 5......
  • Element 关闭图标区分关闭、取消、ESC
    使用distinguishCancelAndClose属性关闭:close取消:cacelESC:closeElement.MessageBox.confirm('是否要离开?','提示',{confirmButtonText:'......
  • 修改web网站标签页的图标和名字
    <head><metacharset="UTF-8"/><linkrel="shortcuticon"type="image/png"href="https://foster.xxxxx.cn/favicon.ico"><linkrel="Bookmark"type="image/png"href="ht......
  • pwa添加图标到桌面
    1、配置sw.js文件注意:sw.js放在根目录,与index.html同级/***serviceworker*/varcacheName='';varapiCacheName='';varcacheFiles=['/','.......
  • 猫猫回收站图标
     文件下载:https://wwz.lanzouw.com/i1GLR05yy2mdRunWith("admin")Gui,Add,Picture,w300h-1,bitbug_favicon(3).icoGui,Add,Picture,x300y5w300h-1,bitb......
  • js点击密码框旁的图标可自由切换密码框和文本框
    点击密码框旁的图标可自由切换密码框和文本框 <divclass="box"><inputtype="password"id="pwd"><labelfor="pwd"><imgsrc="image......
  • 【工具】在线编辑svg
    推荐网站https://www.drawsvg.org/drawsvg.html其他推荐https://juejin.cn/post/6844903616138969102#comment......
  • 把网站变成exe程序,还能自定义图标
    把网站变成exe程序,还能自定义图标把JShaman网站变成exe程序概述:本文将演示如何把一个网站转换成一个exe软件程序,以JShaman网站为例,而本方法适合转换任意网站,还能自定义图标......