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 圖標選擇器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue使用原生js創(chuàng)建元素樣式不生效問題及解決
這篇文章主要介紹了vue使用原生js創(chuàng)建元素樣式不生效問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06

