Vue3 封装 element-plus 图标选择器实现步骤

 更新时间:2023年09月19日 09:56:32   作者:大可-  
这篇文章主要介绍了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 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>

到此这篇关于Vue3 封装 element-plus 图标选择器的文章就介绍到这了,更多相关Vue3 element-plus 图标选择器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue实现点击当前标签高亮效果【推荐】

    vue实现点击当前标签高亮效果【推荐】

    这篇文章主要介绍了vue实现点击当前标签高亮效果的思路详解,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友参考下吧
    2018-06-06
  • vue实现计步器功能

    vue实现计步器功能

    这篇文章主要为大家详细介绍了vue实现计步器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • Vue中的 mixins 和 provide/inject详解

    Vue中的 mixins 和 provide/inject详解

    这篇文章主要介绍了Vue中的 mixins 和 provide/inject详解,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-07-07
  • vue 插件的方法代码详解

    vue 插件的方法代码详解

    在开发项目的时候,我们一般都用 vue-cli 来避免繁琐的 webpack 配置和 template 配置。这篇文章主要介绍了如何写一个 vue 插件,需要的朋友可以参考下
    2019-06-06
  • vue.js添加一些触摸事件以及安装fastclick的实例

    vue.js添加一些触摸事件以及安装fastclick的实例

    今天小编就为大家分享一篇vue.js添加一些触摸事件以及安装fastclick的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • 基于vue3与supabase系统认证机制详解

    基于vue3与supabase系统认证机制详解

    这篇文章主要介绍了基于vue3与supabase系统认证机制,,系统使用基于 JWT (JSON Web Token) 的认证方式,提供了安全可靠的用户身份管理机制,需要的朋友可以参考下
    2025-04-04
  • Vue之绑定class样式与style样式方式

    Vue之绑定class样式与style样式方式

    这篇文章主要介绍了Vue之绑定class样式与style样式方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • Element ui中menu组件(el-menu/el-menu-item/el-submenu/template)层级结构与用法示例

    Element ui中menu组件(el-menu/el-menu-item/el-submenu/template)

    最近在使用Element开发时遇到了不少问题,下面这篇文章主要给大家介绍了关于Element ui中menu组件(el-menu/el-menu-item/el-submenu/template)层级结构与用法的相关资料,需要的朋友可以参考下
    2022-12-12
  • Vue3中ref和reactive的使用场景详解

    Vue3中ref和reactive的使用场景详解

    这篇文章主要介绍了Vue3中ref和reactive的使用场景,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-04-04
  • iphone刘海屏页面适配方法

    iphone刘海屏页面适配方法

    这篇文章主要介绍了iphone刘海屏页面适配方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05

最新评论