el-select 添加icon

Element-ui-plus 新版本:Select 选择器 | Element Plus

要实现如上的效果 ,要用到具名插槽 prefix,看代码:

复制代码
<template>
    <el-dialog ref="dialogRef" v-model="dialogVisible" :title="title" :width="dialogWidth" :before-close="handleClose" :close-on-click-modal="closeModal" :append-to-body="appendToBody" :class="['resizable-dialog', { 'is-dragging': isDragging }]" draggable>
        <template #header>
            <div class="dialog-header flex flex-col justify-center items-center">
                <div class="dialog-title font-bold text-gray-100">Transfer Crypto</div>
                <div class="text-gray-500"> Balence: $33.96</div>
            </div>
        </template>
        <el-form label-position="top">
            <el-form-item label="TOKEN">
                <el-select v-model="voData.form.chain" placeholder="Select Chain">
                    <el-option v-for="item in voState.chainOptions" :key="item.value" :label="item.label" :value="item.value">
                        <div class="flex items-center gap-1">
                            <svg-icon class="w-3 h-3 ml-2" :name="item.icon" />
                            <span :style="{ color: item.value }">{{ item.label }}</span>
                        </div>
                    </el-option>
                    <template #prefix>
                        <svg-icon class="w-3 h-3 ml-2" :name="selectedIcon"></svg-icon>
                    </template>
                </el-select>
            </el-form-item>

        </el-form>
    </el-dialog>

</template>
    
    <script setup lang="ts">
import { ref, reactive, computed, onMounted, onUnmounted } from "vue";
import SvgIcon from "../SvgIcon/SvgIcon.vue";

// 定义 props
const props = defineProps({
    title: {
        type: String,
        default: "标题",
    },
    visible: {
        type: Boolean,
        default: false,
    },
    closeModal: {
        type: Boolean,
        default: false,
    },
    appendToBody: {
        type: Boolean,
        default: true,
    },
});

// 定义 emits
const emit = defineEmits([
    "update:visible",
    "close",
    "cancel",
    "confirm",
    "save",
]);

// 弹窗显示状态
const dialogVisible = computed({
    get: () => props.visible,
    set: (val) => emit("update:visible", val),
});

// 弹窗宽度
const dialogWidth = ref("380px");

const dialogRef = ref(null); // ref 引用

const voState = reactive({
    chainOptions: [
        {
            label: "USDC",
            value: "USDC",
            icon: "usdc",
        },
        {
            label: "DAI",
            value: "DAI",
            icon: "dai",
        },
        {
            label: "ETH",
            value: "ETH",
            icon: "eth",
        },
        { label: "USDC.e", value: "USDC.e", icon: "usdc" },
        { label: "USDT", value: "USDT", icon: "usdt" },
        { label: "POL", value: "POL", icon: "pol" },
        { label: "WETH", value: "WETH", icon: "weth" },
    ],
});

const voData = reactive({
    form: {
        chain: "",
    },
});

// 根据选中的值动态计算图标
const selectedIcon = computed(() => {
  const selectedOption = voState.chainOptions.find(option => option.value === voData.form.chain);
  return selectedOption ? selectedOption.icon : null;
});

// 关闭弹窗
const close = () => {
    emit("close");
    dialogVisible.value = false;
};

// 取消
const cancel = () => {
    emit("cancel");
    dialogVisible.value = false;
};

// 确认
const confirm = () => {
    emit("confirm");
};

// 保存
const save = () => {
    emit("save");
};

// 处理关闭事件
const handleClose = () => {
    emit("cancel");
    dialogVisible.value = false;
};

// 监听窗口大小变化
onMounted(() => {});

// 移除监听器
onUnmounted(() => {});
</script>
    
    <style lang="scss" scoped>
</style>
相关推荐
太平洋月光2 小时前
AI 快捷指令:Cursor Rules · Commands · Skills
前端·ai编程
用户83134859306982 小时前
Cesium 实现行政区反向遮罩镂空效果(自定义暗色蒙层+矢量/影像底图切换)
vue.js·webgl·cesium
默_笙2 小时前
😁 LLM 不知道的事它不会说"不知道",而是会"编"——RAG 就是让它闭嘴先查资料
前端·javascript
stringwu2 小时前
用 LangGraph 落地飞书 Bug 自动修复 Agent 实践
前端
Revolution612 小时前
变量提升到底提升了什么:为什么 var 是 undefined,let 却直接报错
前端·javascript
labixiong2 小时前
ES2026 落地了什么?两个真上线的特性 + 两个被毙的,一次讲清楚
javascript·ecmascript 6·ecmascript 8
Revolution612 小时前
本地明明正常,为什么 CI 又挂了:一次前端构建失败的排查过程
前端·前端工程化
太平洋月光2 小时前
stagewise如何结合cursor开发
前端·ai编程
前端Hardy2 小时前
JavaScript 终于又进化了!ES2026 正式发布,这些新特性你必须知道!
前端
光影少年2 小时前
react navite 页面跳转、传参、路由监听、导航栏自定义
前端·react native·react.js