Vue3 封装 element-plus 图标选择器

这篇具有很好参考价值的文章主要介绍了Vue3 封装 element-plus 图标选择器。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、实现效果

效果一:

Vue3 封装 element-plus 图标选择器,前端,vue.js,javascript

效果二:

Vue3 封装 element-plus 图标选择器,前端,vue.js,javascript 

效果一的这个是把全部的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图标文章来源地址https://www.toymoban.com/news/detail-733765.html

三、效果二实现步骤

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>

到了这里,关于Vue3 封装 element-plus 图标选择器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • vue3 vue.config.js配置Element-plus组件和Icon图标实现按需自动引入

    打包时,报警告,提示包太大会影响性能 在页面直接使用,直接使用 SVG 图标,当做一般的 svg 使用 icon使用时需要用以下两种方式方式: 如果用在el-button里面的icon属性上使用,用SVG方式无效,还是需要引入再使用(不知道有没有其他方式) 注意: 使用 :icon=\\\"Edit\\\" 则icon的大

    2024年02月06日
    浏览(43)
  • 二、搭建前后端分离的自动化测试平台的前端Vue3+Element-plus前端项目

    1、Node获取地址 https://nodejs.org/en/download 一直默认选项安装,安装好了之后,在环境变量中会自动配置Node的地址,可以在cmd中使用 node -v/npm -v命令验证是否下载成功 2、设置Node的配置内容 (1)在安装目录下新建两个文件夹命名为node_cache,和node_global: 其中 node_cache 是作为 缓

    2024年02月06日
    浏览(43)
  • Vue3 + TS + Element-Plus —— 项目系统中封装表格+搜索表单 十分钟写五个UI不在是问题

    前期回顾 纯前端 —— 200行JS代码、实现导出Excel、支持DIY样式,纵横合并-CSDN博客 https://blog.csdn.net/m0_57904695/article/details/135537511?spm=1001.2014.3001.5501 目录 一、🛠️  newTable.vue 封装Table 二、🚩 newForm.vue 封装搜索表单  三、📝 TS类型 srctypesglobal.d.ts 四、♻️ 页面使用功能

    2024年01月24日
    浏览(45)
  • Vue3+Vue-Router+Element-Plus根据后端数据实现前端动态路由——权限管理模块

    提示:文章内容仔细看一些,或者直接粘贴复制,效果满满 提示:文章大概 1、项目:前后端分离 2、前端:基于Vite创建的Vue3项目 3、后端:没有,模拟的后端数据 4、关于路径“@”符号——vite.config.js 文件里修改 提示:以下是本篇文章正文内容,下面案例可供复制粘贴使用

    2024年02月02日
    浏览(38)
  • Vue3.2 + TypeScript + Pinia + Vite4 + Element-Plus + 微前端(qiankun) 后台管理系统模板(已开源---显示项目页面截图)

    Wocwin-Admin,是基于 Vue3.2、TypeScript、Vite、Pinia、Element-Plus、Qiankun(微前端) 开源的一套后台管理模板;同时集成了微前端 qiankun也可以当做一个子应用。项目中组件页面使用了Element-plus 二次封装 t-ui-plus 组件,目前已新增fastmock接口。 Link:https://wocwin.github.io/wocwin-admin/ 账号:

    2024年02月08日
    浏览(48)
  • vue3使用element-plus

    element-ui 是配合 vue2 使用,element-plus 是配置 vue3 使用的 1. 包管理器的方式 如果是使用 webpack 或者 vite 打包工具新建的项目 2. 浏览器直接导入 直接通过浏览器的 HTML 标签导入 Element Plus,然后就可以使用全局变量 ElementPlus 1. 导入全部组件且注册所有的图标 声明使用 ElementPl

    2024年02月08日
    浏览(56)
  • Vue3导入Element-plus方法

    先引入依赖 main.js中要引入两个依赖 然后 这个东西 我们最好还是挂载vue上 所以 还是 然后 我们可以在组件上试一下用一个ElementUi的表格组件 参考代码如下 运行结果如下 也是没有任何问题

    2024年02月06日
    浏览(31)
  • vue3 element-plus 实现图片预览

    element-plus下有这么一个组件 el-image-viewer /,但是这个组件是没写在文档上面的,像普通组件一样使用即可 可以通过点击按钮实现图片预览,而非el-image组件只能通过点击图片实现预览 2.1封装组件 2.3组件使用 在需要使用的地方引入,然后使用即可,这不是重点,每个人使用的

    2024年02月15日
    浏览(44)
  • vue 基于element-plus el-button封装按钮组件

    封装组件的原则是:组件只是数据流通的一个管道,不要糅合太多的逻辑在里面,是一个纯组件,还要根据自己项目的业务场景做具体的处理。

    2024年02月10日
    浏览(34)
  • vue3+element-plus上传文件,预览文件

    vue3+ts+element-plus上传文件,预览文件 场景:使用element-plus的el-upload标签,手动上传文件,可预览docx,xlsx,pdf,jpg,jpeg,png(本地资源以及网络资源)。 1、使用el-upload标签 检查上传文件的文件格式与大小 上传的附件信息在fileList中,组装接口所需数据进行上传 使用docx-preview插件预览

    2024年02月11日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包