vue3 const d= reactive({a: [{ name: ““, val: ““ }]} 在push去重

import { reactive, toRaw } from 'vue';
 
const state = reactive({
  items: []
});
 
const addItem = (item) => {
  const rawState = toRaw(state);
  const existingItemIndex = rawState.items.findIndex(i => i.name === item.name);
  if (existingItemIndex !== -1) {
    // 如果已存在,更新值
    rawState.items[existingItemIndex].val = item.val;
  } else {
    // 如果不存在,直接push
    state.items.push(item);
  }
};
 
// 使用示例
addItem({ name: 'item1', val: 'value1' });
addItem({ name: 'item2', val: 'value2' });
addItem({ name: 'item1', val: 'newValue1' }); // 更新item1的值

在这个例子中,addItem函数首先将响应式状态转换成原始对象,然后检查该数组中是否已存在具有相同name属性的对象。如果存在,则更新它的val属性;如果不存在,则将新项目push到数组中。这样可以确保state.items数组中不会有重复的项目。

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
看起来你的代码中没有使用 `onChangeOption` 方法,所以父组件的数据不会更新。你可以在 `WCheckboxGroup` 子组件中触发 `onChangeOption` 方法来更新父组件的数据。 你可以在 `WCheckboxGroup` 组件中添加一个 `watch` 监听 `value` 的变化,并在变化时触发 `onChangeOption` 方法。修改后的代码如下: ```vue <template> <div> <div v-for="(option, index) in options" :key="index"> <label> <input type="checkbox" v-model="option.checked" :value="option.value" @change="onChange" /> {{ option.label }} </label> </div> </div> </template> <script lang="ts"> import { defineComponent, toRefs, watch } from "vue"; export default defineComponent({ name: "WCheckboxGroup", props: { value: { type: Array, default: () => [], }, options: { type: Array, default: () => [], }, }, setup(props, { emit }) { const onChange = (event: Event) => { const target = event.target as HTMLInputElement; const value = target.value; const checked = target.checked; let newValue = [...props.value]; if (checked) { newValue.push(value); } else { newValue = newValue.filter((item) => item !== value); } emit("update:value", newValue); }; watch(props.value, (newValue) => { emit("changeOption", newValue); }); return { ...toRefs(props), onChange, }; }, }); </script> ``` 然后在父组件中,你可以监听 `WCheckboxGroup` 组件的 `changeOption` 事件,并在事件处理函数中更新 `checkedList` 的值。修改后的代码如下: ```vue <template> <div> <div> <div>封装后的多选框</div> <div> <w-checkbox-group v-model:value="checkedList" :options="plainOptions" @changeOption="onChangeOption" /> </div> </div> </div> </template> <script lang="ts"> import WCheckboxGroup from "@/components/w-checkbox-group/checkboxGroup.vue"; import { defineComponent, reactive, toRefs } from "vue"; const plainOptions = [ { label: "Apple", value: "Apple" }, { label: "Pear", value: "Pear" }, { label: "cc", value: "cc" }, ]; export default defineComponent({ components: { WCheckboxGroup, }, setup() { const state = reactive({ indeterminate: true, checkAll: false, checkedList: ["Apple"], }); const onChangeOption = (val: any) => { state.checkedList = val; }; return { ...toRefs(state), plainOptions, onChangeOption, }; }, }); </script> ``` 这样修改后,当子组件中选项的选中状态发生变化时,父组件的 `checkedList` 数据也会更新,并新渲染视图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值