【前端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>
相关推荐
FRing032 小时前
vue2干货总结7(自定义指令、插槽、单页应用程序、路由的使用(VueRouter))
开发语言·前端·vue.js·vue2
行者-全栈开发3 小时前
【鸿蒙心迹】国际化 i18n 与多语言资源管理实战(HarmonyOS 7.x)
ui·华为·重构·国际化·harmonyos·多语言
疯华整猫5 小时前
被杀掉之后:我把 Android 的「唤起 + 常驻」重做了一遍
前端·面试·架构
ruanno_top5 小时前
点全选,已选的被清空了:Transfer 三态复选框的三个 bug
vue.js
时间的情敌6 小时前
Cesium 加载 3D Tiles 倾斜摄影、压平、样式设置(Vue3 实战)
vue.js·3d·cesium
IT_陈寒7 小时前
Java开发实战:从入门到精通
前端·人工智能·后端
风骏时光牛马7 小时前
生成一个AI转岗经历标题,只需要一个标题,不需要其他的任何文本。
前端
Csvn16 小时前
Monorepo
前端
Csvn17 小时前
流水线
前端
Csvn17 小时前
微前端架构
前端