父
javascript
<template>
<div>
<MySelect v-model="data" />
<div>
code1: {{ data.code1 }}
</div>
<div>
code2: {{ data.code2 }}
</div>
</div>
</template>
<script setup>
import { countdownEmits } from 'element-plus';
import MySelect from './component/mySelect/index.vue';
import { ref, reactive, onMounted } from 'vue';
let data = ref({
code1: '1',
code2: 'Option2',
});
onMounted(() => {})
</script>
<style></style>
子
javascript
<template>
<div>
<el-select v-model="localValue.code1" @change="change">
<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
</el-select>
<el-select v-model="localValue.code2" @change="change">
<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
</el-select>
</div>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue';
let emits = defineEmits(['update:modelValue'])
const props = defineProps({
modelValue: {
type: Object,
default() {
return {
code1: '1',
code2: '2'
}
}
}
})
const localValue = reactive({ ...props.modelValue }); // 拷贝 modelValue
const options = ref([])
const change = () => {
emits('update:modelValue', localValue)
}
onMounted(() => {
console.log('值', props.modelValue)
setTimeout(() => {
options.value = [
{
value: 'Option1',
label: 'Option1',
},
{
value: 'Option2',
label: 'Option2',
},
{
value: 'Option3',
label: 'Option3',
},
]
}, 10000)
})
</script>
<style scoped></style>