一开始我是这样做的,但是这样打印的结果是value是undefined,index是正常的
<div v-for="(item, index) in formState.List" :key="index"><a-form-item label="自定义周"><a-selectv-model:value="formState.List[index].Week"mode="multiple":options="[{ value: '星期一',},{ value: '星期二',},]"@change="onChangeRangeWeek(value, index)"></a-select></a-form-item>
</div><script>
const formState = reactive({List: [{type: "",week: [],timeStart: "",timeEnd: "",},],
})
// 获取选择的周
const onChangeRangeWeek = (value: string[], index: number) => {console.log('value---',value) // undefinedconsole.log('index---',index) // 0
};
</script>
正确的做法是利用回调函数来传递
<div v-for="(item, index) in formState.List" :key="index"><a-form-item label="自定义周"><a-selectv-model:value="formState.List[index].Week"mode="multiple":options="[{ value: '星期一',},{ value: '星期二',},]"@change="(value) => onChangeRangeWeek(value, index)"></a-select></a-form-item>
</div><script>
const formState = reactive({List: [{type: "",week: [],timeStart: "",timeEnd: "",},],
})
// 获取选择的周
const onChangeRangeWeek = (value: string[], index: number) => {formState.List[index].Week = value;
};
</script>