【前端element-ui】对于封装el-select和checkbox-group的多选控件导致数据双向绑定失败问题的处理

一、关于通过父组件props传参el-select【下拉多选模式】双向绑定失败问题处理方式

1、this.$emit("change", val); 采用change二不是input

2、对_value赋值不能直接使用"="号,而是push

html 复制代码
<template>
  <div>
      <el-select v-model="_value" multiple clearable placeholder="请选择选项" @change="handleValue">
        <el-option v-for="(v, index) in options" :key="index" :value="v" :label="v"></el-option>
      </el-select>
  </div>
</template>
<script>

export default {
  name: "multiple",
  props:{
    value:{
      type: Array,
      default: () => {
        return []
      }
    },
    options:{
      type: Array,
      default: () => {
        return []
      }
    },
  },
  computed:{
    _value: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit("change", val);
      }
    }
  },
  methods: {
    handleValue(data){
      this._value.splice(0, this._value.length);
      data.forEach(el=>{
        this._value.push(el)
      })
    }
  }
}
</script>

二、关于通过父组件props传参checkbox-group【多选框】双向绑定失败问题处理方式

1、内部事件采用@input而不是@change="handleValue"

2、对_value赋值不能直接使用"="号,而是push

html 复制代码
<template>
  <div>
      <el-checkbox-group v-model="_value" @input="handleValue">
        <el-checkbox v-for="(v, index) in options" :key="index" :label="v" >{{v}}</el-checkbox>
      </el-checkbox-group>
  </div>
</template>
<script>

export default {
  name: "checkbox",
  props:{
    value:{
      type: Array,
      default: () => {
        return []
      }
    },
    options:{
      type: Array,
      default: () => {
        return []
      }
    },
  },
  computed:{
    _value: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit("change", val);
      }
    }
  },
  methods: {
    handleValue(data){
      this._value.splice(0, this._value.length);
      data.forEach(el=>{
        this._value.push(el)
      })
    }
  }
}
</script>
相关推荐
持久的棒棒君8 分钟前
ElementUI 2.x 输入框回车后在调用接口进行远程搜索功能
前端·javascript·elementui
2401_8572979118 分钟前
秋招内推2025-招联金融
java·前端·算法·金融·求职招聘
一 乐23 分钟前
租拼车平台|小区租拼车管理|基于java的小区租拼车管理信息系统小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·微信·notepad++·拼车
undefined&&懒洋洋1 小时前
Web和UE5像素流送、通信教程
前端·ue5
RangoLei_Lzs2 小时前
C++模版SFIANE应用踩的一个小坑
java·开发语言·ui
大前端爱好者3 小时前
React 19 新特性详解
前端
小程xy3 小时前
react 知识点汇总(非常全面)
前端·javascript·react.js
随云6323 小时前
WebGL编程指南之着色器语言GLSL ES(入门GLSL ES这篇就够了)
前端·webgl
随云6323 小时前
WebGL编程指南之进入三维世界
前端·webgl
寻找09之夏4 小时前
【Vue3实战】:用导航守卫拦截未保存的编辑,提升用户体验
前端·vue.js