• vue项目中使用svg


    背景

    一般html中使用svg图片是用img标签使用,这在项目中就相对来说非常麻烦。出现大量使用svg情况就变的比较麻烦

    <img src="../assets/svgs/car.svg" />
    
    • 1

    封装

    svg文件统一放一个文件夹下 src/assets/svgs

    需要2步

    为文件打包

    安装开发依赖

    npm i svg-sprite-loader -D
    
    • 1

    打包配置

    ....
    chainWebpack: config => {
        config.module
          .rule('svg')
          .exclude.add(resolve('src/assets/svgs'))
          .end()
        config.module
          .rule('icons')
          .test(/\.svg$/)
          .include.add(resolve('src/assets/svgs'))
          .end()
          .use('svg-sprite-loader')
          .loader('svg-sprite-loader')
          .options({
            symbolId: 'icon-[name]'
          })
          .end()
      },
    ....
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    ...
    const { defineConfig } = require('@vue/cli-service')
    module.exports = defineConfig({
      chainWebpack: (config) => {
        const svgRule = config.module.rule('svg')
        svgRule.uses.clear()
        // 添加要替换的 loader
        svgRule.use('svg-sprite-loader')
               .loader('svg-sprite-loader')
               .options({symbolId: 'icon-[name]'})
      }
      })
      ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    为开发

    封装VSvg.vue

    
    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    util.js

    export function isExternal (path) {
      return /^(https?:|mailto:|tel:)/.test(path)
    }
    
    export function importAll (requireContext) {
      requireContext.keys().forEach(requireContext)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    关于require.context方法

    核心就是使用了webpack内置的require.context方法
    详细参数解释点击这里官网传送门

    require.context(
      directory, //这里指存放svg文件目录的路径
      (useSubdirectories = true), //是否还搜索其子目录,默认为true
      (regExp = /^\.\/.*$/), //正则匹配文件类型, 这里我们匹配 .svg 结尾的文件 /\.svg$/
      (mode = 'sync') //模式,默认同步
    );
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    main中注册

    import VSvg from  '@/components/base/VSvg.vue'
    import Vue from 'vue'
     Vue.component('v-svg', VSvg)
    
    • 1
    • 2
    • 3

    由于此处我注册的组件较多,而且都在一个文件里面

    const registerGlobal = (Vue) => {
      const requireComponent = require.context(
        '@/components/base', true, /\.vue$/
      )
      console.log('学习', requireComponent)
      requireComponent.keys().forEach(fileName => {
        const componentConfig = requireComponent(fileName)
        const Component = componentConfig.default || componentConfig
        const componentName = kebabCase(fileName.replace(/^\.\//, '').replace(/\.\w+$/, ''))
        if (typeof Component.install === 'function') {
          Vue.use(Component)
        } else {
          Vue.component(componentName, Component)
        }
      })
    
      Vue.directive('perm', hasPerm)
    
      Vue.filter('dayjs', (val, formatter = 'YYYY-MM-DD HH:MM:ss') => dayjs(val).format(formatter))
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    组件使用

      
    
    • 1
  • 相关阅读:
    大数据安全建设面临哪些挑战
    【附源码】Python计算机毕业设计全国生鲜溯源平台
    Jenkins学习笔记4
    如何创建一个自己的sphinx文档网站
    行业落地呈现新进展 | 2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛圆满召开
    SpringBoot项目实战笔记:电脑商城项目实战(SpringBoot+MyBatis+MySQL)
    美元兑换日元价格分析:日图均线形成熊叉
    PostgreSQL的学习心得和知识总结(九十一)|深入理解PostgreSQL数据库开源MPP扩展Citus的安装过程及其Makefile的理解
    Flutter 数据存储--shared_preferences使用详情
    关于面试题
  • 原文地址:https://blog.csdn.net/wujianyouhun/article/details/134074486