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>
目录
相关文章
|
安全 前端开发 Java
Spring Security系列教程25--解决Spring Security环境中的跨域问题
前言 上一章节中,一一哥 给各位讲解了同源策略和跨域问题,以及跨域问题的解决方案,在本篇文章中,我会带大家进行代码实现,看看在Spring Security环境中如何解决跨域问题。 一. 启用Spring Security 的CORS支持 1. 创建web接口 我先在SpringBoot环境中,创建一个端口号为8080的web项目,注意这个web项目没有引入Spring Security的依赖包。然后在其中创建一个IndexController,定义两个测试接口以便被ajax进行跨域访问。 @RestController public class IndexController {
1987 1
|
JavaScript 前端开发 API
core-js介绍及安装使用
core-js介绍及安装使用
core-js介绍及安装使用
关于 QGLWidget和QOpengGLWidget透明相关问题 的解决方法
关于 QGLWidget和QOpengGLWidget透明相关问题 的解决方法
关于 QGLWidget和QOpengGLWidget透明相关问题 的解决方法
|
存储 资源调度
在 Pinia 中如何实现状态持久化?
在 Pinia 中如何实现状态持久化?
3704 57
|
11月前
|
存储 人工智能 搜索推荐
不懂向量数据库?别怕!一文讲清8大主流工具,手把手教你做选择
向量数据库是AI应用的“超级记忆中枢”,能将文本、图像等转化为数学指纹并快速检索相似内容。本文通俗解析8大主流向量数据库,涵盖托管型、开源型与嵌入式三类,助你根据场景选型,轻松构建智能搜索、推荐系统与RAG应用。
6435 6
|
JavaScript
Vue3 + Vite + TS项目引入iconfont图标(Svg方式)
前言 每一个项目都避免不了使用各种各样的图标,如果我们使用了 UI 组件库,比如说 ELement 等,那么组件库有一些封装好的图标供我们使用。但是项目是多变的和复杂的,组件库提供的图标很多时候不能满足需求,这个时候就需要我们自己引入想要的图标了。 今天介绍的便是如何将 iconfont 阿里图标库的图标引入到我们的 Vue3 项目中来!
6550 1
Vue3 + Vite + TS项目引入iconfont图标(Svg方式)
|
人工智能 Java 机器人
基于Spring AI Alibaba + Spring Boot + Ollama搭建本地AI对话机器人API
Spring AI Alibaba集成Ollama,基于Java构建本地大模型应用,支持流式对话、knife4j接口可视化,实现高隐私、免API密钥的离线AI服务。
8103 2
基于Spring AI Alibaba + Spring Boot + Ollama搭建本地AI对话机器人API
|
前端开发 JavaScript 应用服务中间件
前端跨域问题解决Access to XMLHttpRequest at xxx from has been blocked by CORS policy
跨域问题是前端开发中常见且棘手的问题,但通过理解CORS的工作原理并应用合适的解决方案,如服务器设置CORS头、使用JSONP、代理服务器、Nginx配置和浏览器插件,可以有效地解决这些问题。选择合适的方法可以确保应用的安全性和稳定性,并提升用户体验。
10135 90
|
JavaScript
vue项目中引入阿里图标iconfont
该文章指导如何在Vue项目中引入并使用阿里图标库Iconfont,包括图标的选取、下载配置文件及在项目中引入和使用图标的具体步骤。