SVG图片选择库组件封装及使用

news/2024/7/10 2:31:56 标签: vue

需求

需求: 在项目中通常需要做菜单管理,想要让左侧菜单好看一点,一般都会选择添加图标,需要自定义选择喜欢的图标,得提供一个有选择项的图标库

延伸需求:在项目中通常可能有好几个图标选择库,可能每个可选择图标库里的图标都不能相同,这就需要我们做区分展示

前置条件

需要使用到svg图片,以前有写下载使用svg图片的文章,可以参考一下
链接:vue中使用svg

图例

1. 菜单图标库

在这里插入图片描述
在这里插入图片描述

2. 场景图标库

在这里插入图片描述
在这里插入图片描述

实现

svg文件

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

可以在svg文件夹的下级创建scene文件夹,在svg文件夹中的svg图片属于菜单图标库(不包含scene文件夹中的图片),scene文件夹中的svg图片属于场景图标库,如果想美观一点可以把菜单图标库的图标用文件夹归纳一下,重要的是icons文件夹下的index.js文件中的匹配

index.js文件

import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon'// svg component

Vue.component('svg-icon', SvgIcon)

const req = require.context('./svg', false, /\.svg$/)
//温馨提示:要想使用scene文件夹中的图片一定要写这个哦
const scenereq = require.context('./svg/scene', false, /\.svg$/) 
const requireAll = requireContext => requireContext.keys().map(requireContext)
requireAll(req)
requireAll(scenereq)

1. 菜单图标组件

组件(@/components/IconSelect)
  1. index.vue文件
<template>
  <div class="icon-body">
    <el-input v-model="name" style="position: relative;" clearable placeholder="请输入图标名称" @clear="filterIcons" @input.native="filterIcons">
      <i slot="suffix" class="el-icon-search el-input__icon" />
    </el-input>
    <div class="icon-list">
      <div v-for="(item, index) in iconList" :key="index" @click="selectedIcon(item)">
        <svg-icon :icon-class="item" style="height: 30px;width: 16px;" />
        <span>{{ item }}</span>
      </div>
    </div>
  </div>
</template>
<script>
import icons from './requireIcons'
export default {
  name: 'IconSelect',
  data() {
    return {
      name: '',
      iconList:icons
    }
  },
  mounted(){
    
  },
  methods: {
    filterIcons() {
      this.iconList = icons
      if (this.name) {
        this.iconList = this.iconList.filter(item => item.includes(this.name))
      }
    },
    selectedIcon(name) {
      this.$emit('selected', name)
      document.body.click()
    },
    reset() {
      this.name = ''
      this.iconList = icons
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .icon-body {
    width: 100%;
    padding: 10px;
    .icon-list {
      height: 200px;
      overflow-y: scroll;
      div {
        height: 30px;
        line-height: 30px;
        margin-bottom: -5px;
        cursor: pointer;
        width: 33%;
        float: left;
      }
      span {
        display: inline-block;
        vertical-align: -0.15em;
        fill: currentColor;
        overflow: hidden;
      }
    }
  }
</style>
  1. requireIcons.js
const req = require.context('../../icons/svg', false, /\.svg$/)
const requireAll = requireContext => requireContext.keys()

const re = /\.\/(.*)\.svg/

const icons = requireAll(req).map(i => {
  return i.match(re)[1]
})

export default icons
使用
<el-form-item label="菜单图标">
  <el-popover placement="bottom-start" width="460" trigger="click" @show="$refs['IconSelect'].reset()">
    <IconSelect ref="IconSelect" @selected="selected"/>
    <el-input slot="reference" v-model="form.icon" placeholder="点击选择图标">
      <svg-icon v-if="form.icon" slot="prefix" :icon-class="form.icon" class="el-input__icon" style="height: 32px;width: 16px;"/>
      <i v-else slot="prefix" class="el-icon-search el-input__icon" />
    </el-input>
  </el-popover>
</el-form-item>
<script>
import IconSelect from '@/components/IconSelect'
export default {
  components: { IconSelect},
  data(){
    return{
       form:{
         icon:undefined
       }
    }
  },
  methods:{
    // 选择图标
    selected(name) {
      this.form.icon = name
    },
  }
}
</script>

2. 场景图标组件

组件(@/components/SceneIconSelect)
  1. index.vue文件
<template>
  <el-popover placement="bottom-start" width="368" trigger="click" popper-class="sence-icon-popover" @show="reset">
    <div class="icon-body">
      <div class="icon-list">
        <div v-for="(item, index) in iconList" :key="index" @click="selectedIcon(item)">
          <svg-icon :icon-class="item" :class="selectIcon===item?'active-svg':'svg'" />
        </div>
      </div>
    </div>
    <div slot="reference" class="sence-icon">
      <svg-icon slot="prefix" :icon-class="selectIcon" class="el-input__icon" style="height:24px;width:24px;fill:#606266;"/>
    </div>
  </el-popover>
</template>
<script>
import icons from './requireIcons'
export default {
  name: 'SceneIcon',
  props:{
    selectIcon:String
  },
  data(){
    return{
      name: '',
      iconList:icons
    }
  },
  methods:{
    filterIcons() {
      this.iconList = icons
      if (this.name) {
        this.iconList = this.iconList.filter(item => item.includes(this.name))
      }
    },
    selectedIcon(name) {
      this.$emit('selected', name)
      document.body.click()
    },
    reset() {
      this.name = ''
      this.iconList = icons
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.icon-body {
  width: 100%;
  .icon-list {
    height: 128px;
    overflow-y: auto;
    padding: 13px;
    div {
      height: 24px;
      line-height: 24px;
      margin: 5px;
      cursor: pointer;
      width: 24px;
      float: left;
      display: flex;
      align-items: center;
      justify-content: center;
      .svg{
        width: 24px;
        height: 24px;
      }
      .active-svg{
        width: 24px;
        height: 24px;
        fill: $--color-mian !important;
      }
      &:hover,&:active,&:focus{
        .svg{
          fill: $--color-mian !important;
        }
      }
    }
  }
}
.sence-icon{
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #DCDFE6;
}
</style>
<style lang="scss">
.sence-icon-popover{
  padding: 0 !important;
  border-radius: 0;
  border: 1px solid #DCDFE6;
  .popper__arrow{
    border-bottom-color: #DCDFE6 !important;
  }
}
</style>
  1. requireIcons.js
const req = require.context('../../icons/svg/scene', false, /\.svg$/)
const requireAll = requireContext => requireContext.keys()

const re = /\.\/(.*)\.svg/

const icons = requireAll(req).map(i => {
  return i.match(re)[1]
})

export default icons
使用
<el-form-item label="场景图标" prop="icon">
  <SceneIconSelect ref="SceneIconSelect" v-model="form.icon" @selected="selected" :selectIcon="form.icon"></SceneIconSelect>
</el-form-item>
<el-form-item label="图标背景色" prop="color" class="picker-color">
  <el-color-picker v-model="form.color" :class="form.color?'color-picker':'none-color-picker'"></el-color-picker>
</el-form-item>
<script>
import SceneIconSelect from '@/components/SceneIconSelect'
export default {
  components: { SceneIconSelect},
  data(){
    return{
      form:{
        icon:'检修', //默认图标
        color:'#0095FF' //默认颜色
      }
    }
  },
  methods:{
    // 选择图标
    selected(name) {
      this.form.icon = name
    },
  }
}
</script>
<style lang="scss" scped>
.picker-color{
  ::v-deep .el-form-item__content{
    height: 32px;
  }
  .color-picker{
    ::v-deep .el-color-picker__trigger{
      padding: 0;
      border: 0;
      border-radius: 0;
      .el-color-picker__color{
        border: 0;
        border-radius: 0;
      }
    }
  }
  .none-color-picker{
    ::v-deep .el-color-picker__trigger{
      padding: 0;
      border: 0;
      border-radius: 0;
      .el-color-picker__color{
        border: 1px solid #DCDFE6;
        border-radius: 0;
      }
    }
  }
}
</style>

http://www.niftyadmin.cn/n/5225663.html

相关文章

Echarts大屏可视化_02 球体模块制作

继续跟着b站大佬pink老师学大屏可视化 球体模块制作 1.球体模块布局 HTML <div class"column"><div class"no"><div class"no-hd"><ul><li>125811</li><li>104563</li></ul></div&g…

【数据结构/C++】栈和队列_循环队列

牺牲一个存储单元来判断队满。 #include<iostream> using namespace std; // 循环队列 #define MaxSize 10 typedef int ElemType; typedef struct {ElemType data[MaxSize];int front, rear; } SqQueue; // 初始化队列 void InitQueue(SqQueue &Q) {// 判断队空 Q.…

pkpmbs 建设工程质量监督系统 文件上传漏洞复现

0x01 产品简介 pkpmbs 建设工程质量监督系统是湖南建研信息技术股份有限公司一个与工程质量检测管理系统相结合的&#xff0c;B/S架构的检测信息监管系统。 0x02 漏洞概述 pkpmbs 建设工程质量监督系统 FileUpOrDown.aspx、/Platform/System/FileUpload.ashx、接口处存在任意文…

事务的状态和ACID特性

事务就是让数据从一个状态到另一个状态的操作 状态 活动的 事务在执行过程中 部分提交的 事务的最后一个操作已经完成&#xff0c;此时造成的影响只是在内存里&#xff0c;但还没刷写磁盘 失败的 处于活动的或者部分提交的状态时&#xff0c;服务器宕机 中止的 处于失败…

Ilya Sutskever:师从Hinton,“驱逐”奥特曼,一个改变AI世界的天才科学

ChatGPT 已经在全球爆火&#xff0c;但大众在两周之前似乎更熟悉Sam Altman&#xff0c;而对另一位创始人 Ilya Sutskever 却了解不多。 直到前几天因为OpenA眼花缭乱的政权争夺大戏&#xff0c;OpenAI 的首席科学家Ilya Sutskever的名字逐渐被世人所知。 Ilya Sutskever在科…

自定义注解的定义及使用场景

文章目录 1. 自定义注解如何使用2. 自定义注解使用场景2.1 自定义注解使用AOP做权限校验2.2 自定义注解使用AOP记录用户操作日志2.3 自定义注解使用AOP记录接口请求时长 1. 自定义注解如何使用 需要使用interface修饰&#xff0c;加上三个元注解 Documented&#xff1a;生成API…

【数据仓库】-- 数据库设计的三个范式

目录 1、什么是数据库设计的范式? 2、数据库范式详解 2.1 1NF 第

LeetCode [中等]3. 无重复字符的最长子串

3. 无重复字符的最长子串 - 力扣&#xff08;LeetCode&#xff09; 给定一个字符串 s &#xff0c;请你找出其中不含有重复字符的 最长子串 的长度。 1. 滑动窗口&#xff08;Sliding Window&#xff09;&#xff1a; 滑动窗口是一种用于处理数组或列表的子数组或子序列的问题…