Vue3 封装 element-plus 图标选择器

简介: Vue3 封装 element-plus 图标选择器

一、实现效果

效果一:

效果二:

效果一的这个是把全部的icon图标都让它显示出来,让我们自己选择说选图标

二、效果一实现步骤

2.1. 全局注册 icon 组件

// main.ts
import App from './App.vue';
import { createApp } from 'vue';
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
const app = createApp(App);
// 全局挂载和注册 element-plus 的所有 icon
app.config.globalProperties.$icons = []
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.config.globalProperties.$icons.push(key)
    app.component(key, component)
}
app.mount('#app');

2.2. 组件实现

<script setup lang="ts">
import { ComponentInternalInstance, defineEmits, defineProps, getCurrentInstance } from 'vue'
const { appContext: {app: { config: { globalProperties } } } } = getCurrentInstance() as ComponentInternalInstance
interface Props {
  modelValue: string
}
const props = defineProps<Props>()
const emits = defineEmits(['update:modelValue'])
</script>
<template>
  <el-popover trigger="focus" :width="256">
    <template #reference>
      <el-button :icon="modelValue">{{ modelValue }}</el-button>
    </template>
    <div class="el-icon-picker">
      <component v-for="icon in globalProperties.$icons" :key="icon"
        :class="[icon, 'icon', { 'icon-active': icon == modelValue }]"
        :is="icon"
        @click="emits('update:modelValue', icon)">
      </component>
    </div>
  </el-popover>
</template>
<style scoped>
.el-icon-picker {
  height: 256px;
  overflow-y: scroll;
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
}
.icon {
  display: inline-block;
  width: 24px;
  height: 24px;
  color: var(--el-text-color-regular);
  font-size: 20px;
  border-radius: 4px;
  cursor: pointer;
  text-align: center;
  line-height: 45px;
  margin: 5px;
}
.icon:hover {
  color: var(--el-color-primary);
}
.icon-active {
  color: var(--el-color-primary);
}
</style>

2.3. 使用

<script setup lang="ts">
import { ref } from 'vue';
import ElIconPicker from './components/el-icon-picker.vue';
const icon = ref<string>('');
</script>
<template>
  <el-form>
    <el-form-item label="图标">
      <ElIconPicker v-model="icon"></ElIconPicker>
    </el-form-item>
  </el-form>
</template>

效果二的这个是渲染后端返回的icon图标

三、效果二实现步骤

3.1. 全局注册 icon 组件

// main.ts
import App from './App.vue';
import { createApp } from 'vue';
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
const app = createApp(App);
// 全局挂载和注册 element-plus 的所有 icon
app.config.globalProperties.$icons = []
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.config.globalProperties.$icons.push(key)
    app.component(key, component)
}
app.mount('#app');

3.2. 组件实现

<template>
  <el-popover trigger="focus" :width="256">
    <template #reference>
      <el-button style="width: 100px" :icon="props.modelValue">{{ modelValue }}</el-button>
    </template>
    <div class="el-icon-picker">
      <component v-for="icon in props.fatherIcon" :key="icon.value"
                 :class="[icon.value, 'icon', { 'icon-active': icon.value == props.modelValue }]"
                 :is="icon.value"
                 @click="emits('update:modelValue', icon.value)">
      </component>
    </div>
  </el-popover>
</template>
<script lang="ts" setup>
interface Props {
  modelValue: string,
  fatherIcon: any[]
}
const props = defineProps<Props>();
console.log(props)
const emits = defineEmits(['update:modelValue']);
</script>
<style scoped>
.el-icon-picker {
  min-height: 20px;
  overflow-y: scroll;
  display: flex;
  flex-wrap: wrap;
}
.icon {
  display: inline-block;
  width: 24px;
  height: 24px;
  color: var(--el-text-color-regular);
  font-size: 20px;
  border-radius: 4px;
  cursor: pointer;
  text-align: center;
  line-height: 45px;
  margin: 5px;
}
.icon:hover {
  color: var(--el-color-primary);
}
.icon-active {
  color: var(--el-color-primary);
}
</style>

3.3. 使用

<script setup lang="ts">
import { ref } from 'vue';
import ElIconPicker from './components/el-icon-picker.vue';
const icon = ref<string>('');
</script>
<template>
  <el-form>
    <el-form-item label="图标">
     <ElIconPicker v-model="ic" :fatherIcon="icon"></ElIconPicker>
    </el-form-item>
  </el-form>
</template>
目录
相关文章
|
JSON 前端开发 JavaScript
AVUE:前端搬砖神器,一套基于vue+elementUI的框架
AVUE:前端搬砖神器,一套基于vue+elementUI的框架
2517 0
AVUE:前端搬砖神器,一套基于vue+elementUI的框架
|
8月前
|
JSON 物联网 API
天气预报免费API接口【IP查询版】使用教程
IP查询天气API是一款免费实用的接口,可根据IP地址自动获取所在地天气预报,支持自定义IP查询。核心功能包括自动识别请求IP、全国IP天气查询,数据源自中国气象局,无日调用上限。提供详细的返回参数及多语言示例代码,适用于网站、APP、物联网设备等应用场景。
2341 0
|
JavaScript 前端开发 API
vue3 v-md-editor markdown编辑器(VMdEditor)和预览组件(VMdPreview )的使用
本文介绍了如何在Vue 3项目中使用v-md-editor组件库来创建markdown编辑器和预览组件。文章提供了安装步骤、如何在main.js中进行全局配置、以及如何在页面中使用VMdEditor和VMdPreview组件的示例代码。此外,还提供了一个完整示例的链接,包括编辑器和预览组件的使用效果和代码。
vue3 v-md-editor markdown编辑器(VMdEditor)和预览组件(VMdPreview )的使用
|
Web App开发 JSON JavaScript
vue学习:chrome 中 vuetools 开发插件 的下载、安装
这篇文章介绍了如何在Chrome浏览器中下载、安装并测试Vue.js开发插件——vue-devtools。
3933 0
vue学习:chrome 中 vuetools 开发插件 的下载、安装
|
JavaScript
Vue3 + Vite + TS项目引入iconfont图标(Svg方式)
前言 每一个项目都避免不了使用各种各样的图标,如果我们使用了 UI 组件库,比如说 ELement 等,那么组件库有一些封装好的图标供我们使用。但是项目是多变的和复杂的,组件库提供的图标很多时候不能满足需求,这个时候就需要我们自己引入想要的图标了。 今天介绍的便是如何将 iconfont 阿里图标库的图标引入到我们的 Vue3 项目中来!
6086 1
Vue3 + Vite + TS项目引入iconfont图标(Svg方式)
|
前端开发 JavaScript 开发者
利用 el-select 和 el-tree 实现树形结构多选框联动功能
本文详细介绍了如何使用ElementUI中的el-select下拉选择器和el-tree树形控件来实现多功能联动选择器,包括多选、删除、搜索、清空选项等功能。通过树形控件展示复杂的层级结构,用户可以通过下拉选择树形节点,实时搜索节点,且支持批量选择和删除功能。文中提供了完整的HTML、JavaScript和CSS代码实现,帮助开发者快速集成此功能。
5841 0
利用 el-select 和 el-tree 实现树形结构多选框联动功能
|
JavaScript
在 Vue 3 中使用 DHTMLX 甘特图组件
本文将介绍如何在 Vue 3 项目中集成 DHTMLX 甘特图组件,详细讲解安装、模块导入以及基本用法。通过示例代码,您将学会如何配置甘特图的任务、样式和交互功能,帮助您在项目中更有效地管理和展示任务时间线。
1984 0
|
前端开发 Java
后端BUG系列之:SpringBoot上传文件太大 报错 Maximum upload size exceeded
这篇文章讨论了SpringBoot应用中上传文件过大导致的错误"Maximum upload size exceeded",并提供了通过修改`application.properties`文件中的上传限制配置来解决这个问题的方法。
Vue3 封装 element-plus 图标选择器
Vue3 封装 element-plus 图标选择器
558 0
|
JavaScript
在Vue3+ElementPlus项目中使用具有懒加载的el-tree树形控件
在Vue 3和Element Plus项目中实现具有懒加载功能的el-tree树形控件,以优化大数据量时的页面性能。
3502 0

热门文章

最新文章