• Vue3 封装 element-plus 图标选择器


    一、实现效果

    效果一:

    效果二:

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

    二、效果一实现步骤

    2.1. 全局注册 icon 组件

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

    2.2. 组件实现

    1. <script setup lang="ts">
    2. import { ComponentInternalInstance, defineEmits, defineProps, getCurrentInstance } from 'vue'
    3. const { appContext: {app: { config: { globalProperties } } } } = getCurrentInstance() as ComponentInternalInstance
    4. interface Props {
    5. modelValue: string
    6. }
    7. const props = defineProps<Props>()
    8. const emits = defineEmits(['update:modelValue'])
    9. </script>
    10. <template>
    11. <el-popover trigger="focus" :width="256">
    12. <template #reference>
    13. <el-button :icon="modelValue">{{ modelValue }}</el-button>
    14. </template>
    15. <div class="el-icon-picker">
    16. <component v-for="icon in globalProperties.$icons" :key="icon"
    17. :class="[icon, 'icon', { 'icon-active': icon == modelValue }]"
    18. :is="icon"
    19. @click="emits('update:modelValue', icon)">
    20. </component>
    21. </div>
    22. </el-popover>
    23. </template>
    24. <style scoped>
    25. .el-icon-picker {
    26. height: 256px;
    27. overflow-y: scroll;
    28. display: flex;
    29. justify-content: space-around;
    30. flex-wrap: wrap;
    31. }
    32. .icon {
    33. display: inline-block;
    34. width: 24px;
    35. height: 24px;
    36. color: var(--el-text-color-regular);
    37. font-size: 20px;
    38. border-radius: 4px;
    39. cursor: pointer;
    40. text-align: center;
    41. line-height: 45px;
    42. margin: 5px;
    43. }
    44. .icon:hover {
    45. color: var(--el-color-primary);
    46. }
    47. .icon-active {
    48. color: var(--el-color-primary);
    49. }
    50. </style>

    2.3. 使用

    1. <script setup lang="ts">
    2. import { ref } from 'vue';
    3. import ElIconPicker from './components/el-icon-picker.vue';
    4. const icon = ref<string>('');
    5. </script>
    6. <template>
    7. <el-form>
    8. <el-form-item label="图标">
    9. <ElIconPicker v-model="icon"></ElIconPicker>
    10. </el-form-item>
    11. </el-form>
    12. </template>

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

    三、效果二实现步骤

    3.1. 全局注册 icon 组件

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

    3.2. 组件实现

    1. <template>
    2. <el-popover trigger="focus" :width="256">
    3. <template #reference>
    4. <el-button style="width: 100px" :icon="props.modelValue">{{ modelValue }}</el-button>
    5. </template>
    6. <div class="el-icon-picker">
    7. <component v-for="icon in props.fatherIcon" :key="icon.value"
    8. :class="[icon.value, 'icon', { 'icon-active': icon.value == props.modelValue }]"
    9. :is="icon.value"
    10. @click="emits('update:modelValue', icon.value)">
    11. </component>
    12. </div>
    13. </el-popover>
    14. </template>
    15. <script lang="ts" setup>
    16. interface Props {
    17. modelValue: string,
    18. fatherIcon: any[]
    19. }
    20. const props = defineProps<Props>();
    21. console.log(props)
    22. const emits = defineEmits(['update:modelValue']);
    23. </script>
    24. <style scoped>
    25. .el-icon-picker {
    26. min-height: 20px;
    27. overflow-y: scroll;
    28. display: flex;
    29. flex-wrap: wrap;
    30. }
    31. .icon {
    32. display: inline-block;
    33. width: 24px;
    34. height: 24px;
    35. color: var(--el-text-color-regular);
    36. font-size: 20px;
    37. border-radius: 4px;
    38. cursor: pointer;
    39. text-align: center;
    40. line-height: 45px;
    41. margin: 5px;
    42. }
    43. .icon:hover {
    44. color: var(--el-color-primary);
    45. }
    46. .icon-active {
    47. color: var(--el-color-primary);
    48. }
    49. </style>

    3.3. 使用

    1. <script setup lang="ts">
    2. import { ref } from 'vue';
    3. import ElIconPicker from './components/el-icon-picker.vue';
    4. const icon = ref<string>('');
    5. </script>
    6. <template>
    7. <el-form>
    8. <el-form-item label="图标">
    9. <ElIconPicker v-model="ic" :fatherIcon="icon"></ElIconPicker>
    10. </el-form-item>
    11. </el-form>
    12. </template>

  • 相关阅读:
    MCU定位问题(二)
    图论岛屿问题DFS+BFS
    替代STM32的GD32,替代KEIL的Eclipse配置---连载1
    数据结构 第七章(查找算法)
    C语言零基础教程(memset,memcpy函数,memmove函数)
    MySQL索引在关联查询中的作用
    【Codecs系列】x265编码器(十三):自带的帧间提前终止算法
    Fortinet Universal SASE能力再度进化!保障用户访问任意应用程序便捷无忧
    什么是懦弱型人格?懦弱的原因和改变方法
    【STM32】标准库-看门狗
  • 原文地址:https://blog.csdn.net/m0_55333789/article/details/132983623