zoukankan      html  css  js  c++  java
  • vue-在公共icon封装组件里使用svg图标

    1.安装svg-sprite-loader。package.json:"svg-sprite-loader": "^3.9.2",

    2.build/webpack.base.conf.js的model.rules新增配置:

    {
            test: /.svg$/,
            loader: 'svg-sprite-loader',
            include: [resolve('src/icons')],
            options: {
              symbolId: 'icon-[name]'
            }
          },
          {
            test: /.(png|jpe?g|gif|svg)(?.*)?$/,
            loader: 'url-loader',
            exclude: [resolve('src/icons')],
            options: {
              limit: 10000,
              name: utils.assetsPath('img/[name].[hash:7].[ext]')
            }
          },

    3.components下新建SvgIcon/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() {
        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>

    4.src下新建icons/svg文件夹和icons/index.js文件,前者放置所有.svg文件,后者内容为:

    import Vue from 'vue'
    import SvgIcon from '@/components/SvgIcon'// 引入svg组件
    
    // 注册全局组件
    Vue.component('svg-icon', SvgIcon)
    // require.context,通过正则匹配到可能的文件,全部引入
    const req = require.context('./svg', false, /.svg$/)
    const requireAll = requireContext => requireContext.keys().map(requireContext)
    requireAll(req)

    5.在main.js中引入的公共js文件里引入icons下所有文件:

    import '@/icons'

    6.页面使用,icon-class就是之前存放在src/svg下的各个.svg文件名称:

    <svg-icon icon-class="warning" />
    <svg-icon icon-class="why" />

    放个链接

  • 相关阅读:
    sql like模糊查询
    mysql没有delete操作,那是delete from操作,
    j详细说明ava于clone办法
    基于ZooKeeper的Dubbo简单抽样登记中心
    度小于所述过程:es.exe
    Android 支付宝钱包手势password裂纹战斗
    How draw a stem -and -leaf &amp; box-plot display by R.or Python
    POJ 2420 A Star not a Tree? (模拟退火)
    记userscripts.org
    基于最简单的FFmpeg包封过程:视频和音频分配器启动(demuxer-simple)
  • 原文地址:https://www.cnblogs.com/wd163/p/14107556.html
Copyright © 2011-2022 走看看