element el-select组件封装

该文章展示了如何在Vue.js中封装一个el-select组件,并实现模糊查询功能。组件使用v-model绑定数据,通过props传递属性如label和value,同时设置了filterable属性以支持模糊搜索。在change事件中,组件会触发自定义事件getValue和getOrgName,分别传递选中的值和label。
摘要由CSDN通过智能技术生成

 el-select组件封装,并且做到模糊查询

// 我这里接口返回的数据 key 是 orgName 和 id,所以写成这样
<SearchAASelect v-model="search.orgId" title="机构名称" :options='options' :filterable='true' :props="{ label: 'orgName', value: 'id' }" />
<template>
  <div class="search-item">
    <label class="search-label">{{title}}:</label>
    <el-select v-model="selectValue" ref="selectLabel" @change="getValue" :filterable='filterable' clearable class="w-220" placeholder="请选择">
      <el-option v-for="item in options" :key="item.value" :label="item[props.label]" :value="item[props.value]" />
    </el-select>
  </div>
</template>

<script>
export default {
  data () {
    return {
      selectValue: ''
    }
  },
  props: {
    value: {
      default: ''
    },
    title: {
      default: ''
    },
    options: {
      default: () => { }
    },
    props: {
      default: () => {
        return { label: 'label', value: 'value' }
      }
    },
    filterable: {
      default: false
    }
  },
  model: {
    prop: 'value',
    event: 'getValue'
  },
  watch: {
    value: {
      handler (v) {
        this.selectValue = v
      },
      deep: true
    }
  },
  created () {
    this.selectValue = this.value
  },
  methods: {
    getValue () {
      this.$emit('getValue', this.selectValue)
      // 获取label
      this.$nextTick(() => {
        this.$emit('getOrgName', this.$refs.selectLabel.selectedLabel)
      })
    }
  }
}
</script>

<style lang="less" scoped>
</style>

在 Vue3 中,你可以通过将 Element UI 的 `el-select` 组件包装成自定义的 Vue 组件来实现复用和功能扩展。下面是一个基本的步骤: 1. **导入组件**: 首先,在你的项目中安装 Element UI,如果还没有安装,可以使用 npm 或 yarn 安装: ``` npm install element-ui ``` 2. **创建组件文件**: 创建一个新的 Vue 文件(例如 `CustomSelect.vue`),并引入 `ElSelect` 组件: ```html <template> <el-select v-model="selectedValue" :options="options"> <!-- 可能需要添加其他属性如 placeholder、clearable 等 --> </el-select> </template> <script> import { ElSelect } from 'element-plus'; export default { components: { ElSelect, }, data() { return { selectedValue: '', options: [], // 这里存储下拉选项数据 }; }, // ...你可以添加生命周期钩子、计算属性、方法等 }; </script> ``` 3. **定制功能**: - 如果需要添加额外的选项、过滤器或者其他特性,可以在 `methods` 或者计算属性中实现。 - 如果想在选中值改变时触发自定义事件,可以定义事件处理器(如 `@change="handleSelectionChange"`)。 4. **在父组件中使用**: 现在,你可以在其他 Vue 组件模板中使用这个自定义的 `CustomSelect` 组件,并传入必要的数据和配置: ```html <custom-select :options="categoryOptions" @change="handleChange"></custom-select> ``` 5. **提供props和emits**: 为了使得组件更加强大,可以考虑暴露一些 props 和 emits 接口,以便外部传递数据和监听事件变化。 记得在项目中注册这个组件,使其可用: ```javascript import CustomSelect from './components/CustomSelect.vue'; Vue.component('custom-select', CustomSelect); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学不会•

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值