惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
博客园_首页
IT之家
IT之家
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Vercel News
Vercel News
美团技术团队
D
Docker
WordPress大学
WordPress大学
T
Tailwind CSS Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
Y
Y Combinator Blog
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
G
Google Developers Blog
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
MongoDB | Blog
MongoDB | Blog
S
SegmentFault 最新的问题
GbyAI
GbyAI
Hugging Face - Blog
Hugging Face - Blog
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans

博客园 - Mc525

antv3 x6 基本语法-流程图(二) element ui el-form 表单错误 滚动可视区域 vue2 element ui el-table大数据处理无分页解决方案-分片加载 vue2 地图热力图 -下钻省市县 三级及地图资源文件 vue2 组件封装 el-input vue2 组件封装 el-date-picker 日期 vue2 scss sass 基础安装包、安装依赖报错 !!! vue2 封装组件使用 v-mode【el-radio,el-input】 Mac Jenkins 环境部署 vue2 按钮权限(七) vue2 换肤(六) vue2 项目实例 国际化(五) vue2 项目实例 动态路由菜单(四) vue2 项目实例 Mock数据模拟(三) vue2 项目实例 Layout布局(二) vue2 项目实例 项目初始化(一) vue2 项目架构--api.js(七) vue2 项目架构--vue.config.js(七) vue2 项目架构--main.js(六)
vue2 组件封装 el-select
Mc525 · 2025-11-26 · via 博客园 - Mc525
<template>
  <el-select
    v-model="selectedValue"
    v-bind="$attrs"
    v-on="$listeners"
    :multiple="multiple"
    :filterable="filterable"
    :remote="remote"
    :remote-method="handleRemote"
    :loading="loading"
    :clearable="clearable"
    :style="selectStyle"
    @change="handleChange"
    @clear="handleClear"
  >
    <!-- 普通选项 -->
    <el-option
      v-for="item in options"
      :key="item[valueKey]"
      :label="item[labelKey]"
      :value="item[valueKey]"
    />
    <slot></slot>
  </el-select>
</template>

<script>
export default {
  name: "CustomSelect",
  props: {
    // 绑定值
    value: {
      type: [String, Number, Array],
      default: "",
    },
    // 选项列表
    options: {
      type: Array,
      default: () => [],
    },
    // 占位符
    placeholder: {
      type: String,
      default: "请选择",
    },
    // 是否禁用
    disabled: {
      type: Boolean,
      default: false,
    },
    // 是否多选
    multiple: {
      type: Boolean,
      default: false,
    },
    // 是否可搜索
    filterable: {
      type: Boolean,
      default: false,
    },
    // 是否远程搜索
    remote: {
      type: Boolean,
      default: false,
    },
    // 远程加载函数
    remoteMethod: {
      type: Function,
      default: () => {},
    },
    // 是否正在加载
    loading: {
      type: Boolean,
      default: false,
    },
    // 是否可清空
    clearable: {
      type: Boolean,
      default: true,
    },
    // 选项的标签字段名
    labelKey: {
      type: String,
      default: "label",
    },
    // 选项的值字段名
    valueKey: {
      type: String,
      default: "value",
    },
    // 自定义样式
    selectStyle: {
      type: Object,
      default: () => {},
    },
  },
  computed: {
    // 双向绑定处理
    selectedValue: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit("input", val);
      },
    },
  },
  methods: {
    // 远程搜索触发
    handleRemote(query) {
      this.$emit("remote", query);
      this.remoteMethod(query);
    },
    // 值改变触发
    handleChange(val) {
      this.$emit("change", val);
    },
    // 清空触发
    handleClear() {
      this.$emit("clear");
    },
  },
};
</script>

<style scoped>
/* 可添加自定义样式 */
.el-select {
  width: 100%;
}
</style>
<my-select  v-model="value1"  :options="options">