英文:
Pass prop to child component with v-model
问题
I have implemented a custom component (InvoiceTypeAndCustomerInfo
) that utilizes a VAutocomplete element. The selected customer is passed to the component via props.
However, I am encountering an issue where the VAutocomplete does not pre-select the customer that is passed through the props when the page is initially loaded. Instead, it appears as if no customer is selected by default, even though the customer prop is correctly passed to the VAutocomplete.
Script in InvoiceTypeAndCustomerInfo
:
const props = defineProps({
invoiceType: {
type: Number,
required: true,
},
customer: {
type: Object,
required: true,
},
})
const customer = ref({ id: 0, fullname: '' })
const customers = ref([{ id: 1, fullname: 'Customer 1' }, { id: 2, fullname: 'Customer 2' }])
Template (AppAutocomplete
is the renamed VAutocomplete):
<AppAutocomplete
v-model="customer"
:items="customers"
item-title="fullname"
item-value="id"
return-object
placeholder="Select Customer"
class="mb-6"
density="compact"
>
The parent uses InvoiceTypeAndCustomerInfo
like this:
<InvoiceTypeAndCustomerInfo
:customer="customer"
:invoice-type="invoiceType"
@customer-changed="customerChanged"
/>
where customer is defined like this:
const customer = ref({ id: 2, fullname: 'Customer 2' })
Why doesn't the VAutocomplete component display the customer from the props?
英文:
I have implemented a custom component (InvoiceTypeAndCustomerInfo
) that utilizes a VAutocomplete element. The selected customer is passed to the component via props.
However, I am encountering an issue where the VAutocomplete does not pre-select the customer that is passed through the props when the page is initially loaded. Instead, it appears as if no customer is selected by default, even though the customer prop is correctly passed to the VAutocomplete.
Script in InvoiceTypeAndCustomerInfo
:
const props = defineProps({
invoiceType: {
type: Number,
required: true,
},
customer: {
type: Object,
required: true,
},
})
const customer = ref({ id: 0, fullname: '' })
const customers = ref([{ id: 1, fullname: 'Customer 1' }, { id: 2, fullname: 'Customer 2' }])
Template (AppAutocomplete
is the renamed VAutocomplete):
<AppAutocomplete
v-model="customer"
:items="customers"
item-title="fullname"
item-value="id"
return-object
placeholder="Select Customer"
class="mb-6"
density="compact"
>
The parent uses InvoiceTypeAndCustomerInfo
like this:
<InvoiceTypeAndCustomerInfo
:customer="customer"
:invoice-type="invoiceType"
@customer-changed="customerChanged"
/>
where customer is defined like this:
const customer = ref({ id: 2, fullname: 'Customer 2' })
Why doesn't the VAutocomplete component display the customer tfrom the props?
答案1
得分: 0
看起来你两次使用了customer
,一次作为引用(ref),一次作为属性(prop)。自动完成使用了引用,其中lastname
为空 - 所以实际上已选择了一个值,只是没有标签来显示。
相反,你可以直接使用属性。只需从AppAutocomplete
中删除引用和v-model,并将其连接到使用customer
属性的方式:
const props = defineProps(['customer'])
const emit = defineEmits(['update:customer'])
在模板中:
<AppAutocomplete
:modelValue="customer"
@update:modelValue="emit('update:customer', $event)"
...
>
现在你可以这样使用:<InvoiceTypeAndCustomerInfo v-model:customer="yourCustomer"/>
或者,你可以重命名引用并使用观察器将属性值复制到内部引用。这取决于你想如何使用组件,特别是如何发出更新事件,这在问题中没有指定。但无论如何,你将使用观察器如下:
const selectedCustomer = ref()
watch(
() => props.customer,
() => selectedCustomer.value = props.customer,
{ immediate: true }
)
和
<AppAutocomplete
v-model="selectedCustomer"
...
>
现在,对InvoiceTypeAndCustomerInfo
上customer
的任何更新都会更新所选的值。
希望有所帮助。
英文:
Looks like you are using customer
twice, once as ref and once as prop. The autocomplete uses the ref, where lastname
is empty - so there actually is a value selected, it just has no label to display.
Instead, you can use the prop directly. Just remove the ref and v-model from the AppAutocomplete, and wire it to work with the customer prop:
const props = defineProps(['customer'])
const emit = defineEmits(['update:customer'])
and in the template:
<AppAutocomplete
:modelValue="customer"
@update:modelValue="emit('update:customer', $event)"
...
>
Now you can do <InvoiceTypeAndCustomerInfo v-model:customer="yourCustomer"/>
Alternatively, you can rename the ref and use a watcher to copy the prop value to the internal ref. This depends on how you want to use the component, particularly how you emit the update event, which is not specified in the question. But in any way, you will use watcher like:
const selectedCustomer = ref()
watch(
() => props.customer,
() => selectedCustomer.value = props.customer,
{immediate: true}
)
and
<AppAutocomplete
v-model="selectedCustomer"
...
Now any update to customer on InvoiceTypeAndCustomerInfo
will update the selected value.
Hope it helps
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论