当前位置: 首页 > news >正文

Vue3 封装 element-plus 图标选择器

一、实现效果

二、实现步骤

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 ComponentInternalInstanceinterface 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>

http://www.lryc.cn/news/175276.html

相关文章:

  • 超详细C语言实现——通讯录
  • zabbix监控添加监控项及其监控Mysql、nginx
  • Docker 部署 MongoDB 服务
  • QUIC协议报文解析(三)
  • pytorch迁移学习训练图像分类
  • SQL 如何提取多级分类目录
  • 从中序遍历和后序遍历构建二叉树
  • 《计算机视觉中的多视图几何》笔记(11)
  • UE5 ChaosVehicles载具研究
  • 数据通信——应用层(域名系统)
  • Visual Studio 更新:远程文件管理器
  • ChatGPT追祖寻宗:GPT-3技术报告要点解读
  • java easyexcel 导出多级表头
  • rar格式转换zip格式,如何做?
  • Java中的构造方法
  • 【Java】fastjson
  • JMeter之脚本录制
  • 计算机网络的相关知识点总结
  • WPF实现轮播图(图片、视屏)
  • 【Vue.js】使用Element搭建首页导航左侧菜单
  • Spring MVC常见面试题
  • Java基础面试题精选:深入探讨哈希表、链表和接口等
  • Spark计算框架
  • mybatis缓存源码分析
  • 机房小探索
  • PHP8的类与对象的基本操作之成员变量-PHP8知识详解
  • phpstudy2016 RCE漏洞验证
  • 【QT】QT事件Event大全
  • 华为云云耀云服务器L实例评测|华为云上安装etcd
  • RDLC动态设置整个表格是否显示