一、實(shí)現(xiàn)效果
效果一:
效果二:
?
效果一的這個是把全部的icon圖標(biāo)都讓它顯示出來,讓我們自己選擇說選圖標(biāo)文章來源:http://www.zghlxwxcb.cn/news/detail-733765.html
二、效果一實(shí)現(xiàn)步驟
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. 組件實(shí)現(xiàn)?
<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="圖標(biāo)">
<ElIconPicker v-model="icon"></ElIconPicker>
</el-form-item>
</el-form>
</template>
效果二的這個是渲染后端返回的icon圖標(biāo)文章來源地址http://www.zghlxwxcb.cn/news/detail-733765.html
三、效果二實(shí)現(xiàn)步驟
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. 組件實(shí)現(xiàn)?
<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="圖標(biāo)">
<ElIconPicker v-model="ic" :fatherIcon="icon"></ElIconPicker>
</el-form-item>
</el-form>
</template>
到了這里,關(guān)于Vue3 封裝 element-plus 圖標(biāo)選擇器的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!