Heim > Fragen und Antworten > Hauptteil
P粉2037924682023-08-31 15:28:23
在我看来,这可以以某种更简单的方式完成。
fooItems
可能应该有一个初始状态“已检查”。
在selectedHandler
中,只需调用emit()
即可。
toggleAll
最终将创建一个与 internalModel
配合使用的函数。
这是一个示例 Vue SFC Playground。< /p>
HomeView.vue:
<script setup lang="ts"> import { ref } from 'vue'; import AppList, { type Item } from './AppList.vue'; const fooItems = ref<Item[]>([ { id: 1, name: 'foo 1', checked: false }, { id: 2, name: 'foo 2', checked: false }, { id: 3, name: 'foo 3', checked: false }, { id: 4, name: 'foo 4', checked: false }, { id: 5, name: 'foo 5', checked: true }, ]); const fooSelected = ref<number[]>([]); fooItems.value.map(item => item.checked && fooSelected.value.push(item.id)) </script> <template> <AppList :items="fooItems" v-model:selected="fooSelected"></AppList> <div>fooselected: {{ fooSelected }}</div> </template>
AppList.view:
<script setup lang="ts"> import { ref } from 'vue'; export interface Item { id: number; name: string; checked: boolean } const props = defineProps<{ items: Item[]; selected: number[] }>(); const emit = defineEmits(['update:selected']); const internalModel = ref(props.selected); const selectedHandler = () => emit('update:selected', internalModel.value); const toggleAll = ($event) => { internalModel.value = []; if ( ($event.target as HTMLInputElement).checked ) { props.items.map(item => internalModel.value.push(item.id)); } emit('update:selected', internalModel.value); }; </script> <template> <label> <input type="checkbox" @change="toggleAll($event)" :checked="internalModel.length === items.length" /> toggle all </label> <ul> <li v-for="item in items" :key="item.id"> <label> <input type="checkbox" :value="item.id" v-model="internalModel" @change="selectedHandler(item.id)" :checked="item.checked"/> <span>{{ item.name }}</span> </label> </li> </ul> internalModel: {{ internalModel }} </template>