欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

阿里图标素材在vuecli中的使用(.svg方式)

程序员文章站 2022-07-14 09:24:09
...

1、在vuecli的src下创建icons文件夹,子目录svg,用于存放阿里的.svg图标文件

阿里图标素材在vuecli中的使用(.svg方式)

package.json中安装:          svg-sprite-loader

 

2.svg 目录主要用于存放 svg 文件,来看一下 index.js 的内容,功能就是把组件注册到全局,方便使用:

在src/icons/index.js中使用webpack的require.context自动引入src/icons下面所有的图标。

import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon'// svg组件

// register globally,把SvgIcon组件注册为名为svg-icon的全局组件
Vue.component('svg-icon', SvgIcon)

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

/**
 * 使用webpack的require.context自动引入src/icons下面所有的图标。
 *
 * require.context(directory,useSubdirectories,regExp)
 * require.context():需要一次性的引入某个文件夹下的所有文件
 形参:
 directory:需要引入文件的目录
 useSubdirectories:是否查找该目录下的子级目录
 regExp:匹配引入文件的正则表达式
 */

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

export default {
  allSvg:  requireAll(req)
}

 3、在 src/components/ 下创建 SvgIcon 组件

<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、在main.js中引入

import '@/icons';//引入svg模板

 

5、在组件中如何使用:

1)  这里就会使用 src/icons/svg/example.svg 文件。

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

 

2) 批量展示效果,可用于配置系统图标

阿里图标素材在vuecli中的使用(.svg方式)

<template>
    <div>
        <el-row :gutter="10">
            <el-col :xs="8" :sm="6" :md="4" :lg="3" :xl="1" v-for="(v,index) in svgList" :key="index">
                <div @click="show(v)" style="padding-bottom:8px;text-align: center">
                    <svg-icon :icon-class="v"></svg-icon>
                    <div>
                        {{v}}
                    </div>
                </div>
            </el-col>
        </el-row>
        <hr>
        <h3>单独使用,在src/icons/index.js中已经把Svgicon注册为全局组件,可以直接使用</h3>
        <svg-icon icon-class="督导审核"></svg-icon>
        <br>
        <p>选择展示区域</p>
        <svg-icon v-for="(x,index) in selectedSvg" :icon-class="x" :key="index"></svg-icon>

    </div>

</template>

<script>
    import svgList from "@/icons/index"

    export default {
        name: "svgStu",
        data() {
            return {
                svgList: [],
                selectedSvg: []
            }
        },
        mounted() {
            console.log(svgList)
            for (var i = 0; i < svgList.allSvg.length; i++) {
                let id = svgList.allSvg[i].default.id.replace('icon-', '');
                this.svgList.push(id)
            }
        },
        methods: {
            show(event) {
                this.$message.success('选择了' + event);
                this.selectedSvg.push(event)
            }
        }
    }
</script>

<style scoped>
    h3 {
        text-align: center;
    }
</style>

 

6、接下来是最重要的一步

首先需要注意的是,通过 vue-cli3 构建的项目可以初始化进行很多选择,我构建的目录更多的是以 *.config.js 的形式存在的。

在根目录下创建一个名为 vue.config.js 文件,接下来的操作都和它有关,先来看一下它完整的代码:

const path = require('path')

function resolve(dir) {
  return path.join(__dirname, './', dir)
}

module.exports = {
  chainWebpack: config => {
    config.plugin('define').tap(args => {
      const argv = process.argv
      const icourt = argv[argv.indexOf('--icourt-mode') + 1]

      args[0]['process.env'].MODE = `"${icourt}"`

      return args
    })
    // svg rule loader
    const svgRule = config.module.rule('svg') // 找到svg-loader
    svgRule.uses.clear() // 清除已有的loader, 如果不这样做会添加在此loader之后
    svgRule.exclude.add(/node_modules/) // 正则匹配排除node_modules目录
    svgRule // 添加svg新的loader处理
      .test(/\.svg$/)
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]',
      })

    // 修改images loader 添加svg处理
    const imagesRule = config.module.rule('images')
    imagesRule.exclude.add(resolve('src/icons'))
    config.module
      .rule('images')
      .test(/\.(png|jpe?g|gif|svg)(\?.*)?$/)
  },
  configureWebpack: {
    devServer: {
      open: true,
      // https: true,
      proxy: {
        '/user': {
          target: 'https://devadminschool.icourt.cc',
        },
        '/live': {
          target: 'https://devadminschool.icourt.cc',
        },
      },
    },
  },
}

 转载于:https://www.cnblogs.com/cat-eol/p/11784125.html

demo:https://download.csdn.net/download/caidingnu/12361199

相关标签: vue