黑豹程序员-ElementPlus选择图标器

ElementPlus组件提供了很多图标svg

如何在你的系统中,用户可以使用呢?

这就是图标器,去调用ElementPlus的icon组件库,展示到页面,用户选择,返回选择的组件名称。

效果

代码

bash 复制代码
<template>
  <el-input @click="iconDialogVisible = true" v-model="currentIconName">
  </el-input>
  <el-dialog
      v-model="iconDialogVisible"
      title="请选择图标"
      width="80%"
      :before-close="handleClose"
      @open="beforeOpen"
  >
    <div style="display: flex;flex-wrap: wrap">
      <div v-for="(name,index) in icons" :index="index" :key="index" style="cursor: pointer; padding: 5px; border: 1px solid rgb(227,232,232);"
           :class="currentIconName === name ? 'red' : ''"
           @click="currentIconName = name"
      >
        <component :is="name" style="width: 1.2rem;height: 1.2rem"></component>
      </div>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="iconDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleOk"
        >确定</el-button
        >
      </span>
    </template>
  </el-dialog>

</template>

<script>
import * as ElIcons from '@element-plus/icons-vue'
import {reactive, ref, toRefs, watch} from "vue";

export default {
  components: {
    ...ElIcons
  },
  name: "Naruto-Icon.vue",
  emits: ['update:iconName'],
  props: {
    iconName: {
      type: String
    }
  },
  setup(props, context) {
    const getData = () => {
      let icons = []
      for (const name in ElIcons) {
        icons.push(name)
      }
      return icons
    }
    const handleClose = () => {
      iconList.iconDialogVisible = false;
    }
    const beforeOpen = () => {

    }
    const handleOk = () => {
      context.emit(`update:iconName`, iconList.currentIconName);
      handleClose();
    }
    const iconList = reactive({
      icons: getData(),
      iconDialogVisible: false,
      currentIconName: 'Aim'
    })
    watch(() => props.iconName,(val) => {
      iconList.currentIconName = val;
    })
    return {
      ...toRefs(iconList),
      handleClose,
      beforeOpen,
      handleOk
    }
  }
}
</script>

<style scoped>
.red{
  background-color: palevioletred;
  color: white;
}
</style>
相关推荐
阿珊和她的猫1 分钟前
IIFE:JavaScript 中的立即调用函数表达式
开发语言·javascript·状态模式
阿珊和她的猫9 分钟前
`require` 与 `import` 的区别剖析
前端·webpack
listhi52016 分钟前
卷积码编码和维特比译码的MATLAB仿真程序
开发语言·matlab
yuan1999723 分钟前
基于主成分分析(PCA)的故障诊断MATLAB仿真
开发语言·matlab
J_liaty23 分钟前
Java版本演进:从JDK 8到JDK 21的特性革命与对比分析
java·开发语言·jdk
智商偏低25 分钟前
JSEncrypt
javascript
谎言西西里28 分钟前
零基础 Coze + 前端 Vue3 边玩边开发:宠物冰球运动员生成器
前端·coze
努力的小郑1 小时前
2025年度总结:当我在 Cursor 里敲下 Tab 的那一刻,我知道时代变了
前端·后端·ai编程
翔云 OCR API1 小时前
发票查验接口详细接收参数说明-C#语言集成完整示例-API高效财税管理方案
开发语言·c#
Chasing Aurora1 小时前
Python后端开发之旅(三)
开发语言·python·langchain·protobuf