在我的父元件中我有類似的東西,
<template> <ProductCounter v-model="formData.productCount" label="product count" /> </template> <script setup> const initialFormData = { productCount: null, firstname: '', surname: '', phone: '', email: '', postcode: '', submittedOnce: false, errors: [] } let formData = reactive({ ...initialFormData }); const clearUI = () => { formData = reactive({ ...initialFormData }); triggerInlineForm.value = false; } </script>
我的子元件看起來像這樣,
<template> <div class="form__row" @reset-counts="resetCount"> <div class="counter__row"> <label>{{ label }}</label> <div class="form__counter"> <button class="form__button--decrease form__button--circle form__button--animate-scale" :disabled="value == 0 || props.disabled" @click.prevent="decreaseCount()"> <i> <FontAwesomeIcon :icon="['fal', 'minus']" /> </i> </button> <input type="text" v-model="value" :disabled="props.disabled" @input="updateQty" placeholder="0"/> <button class="form__button--increase form__button--circle form__button--animate-scale" :disabled="props.disabled" @click.prevent="increaseCount()"> <i> <FontAwesomeIcon :icon="['fal', 'plus']" /> </i> </button> </div> </div> </div> </template> <script setup> import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'; const emits = defineEmits(['update:modelValue', 'resetCounts']); const props = defineProps({ label: { type: String, required: true }, modelValue: { type: String, required: true, default: 0 }, disabled: { type: Boolean, required: false } }); const value = ref(props.modelValue); const updateQty = () => { emits('update:modelValue', value.value) } const increaseCount = () => { value.value++ emits('update:modelValue', value.value) } const decreaseCount = () => { value.value--; emits('update:modelValue', value.value) } </script>
我希望當從父級觸發clearUI並且formData被重置時,ProductCounter的v模型應該反映返回到0,但事實並非如此,我哪裡出錯了?
P粉6183582602023-09-14 00:46:50
請下次在 https://play.vuejs.org/ 上準備最低限度的可重現範例。對於你的問題:
請不要覆寫 Vue 中的響應式變數...
只要改變它們Object.assign(formData,initialFormData)
:
也不要取消引用元件屬性:const value = ref(props.modelValue)
。這些屬性失去了它們的反應性,因為您只是複製了一個原始值。
建立 v-model
模式的最佳方法是使用計算
,您可以直接在模板中操作它。
const value = computed({ get(){ return props.modelValue; }, set(val){ emits('update:modelValue', val); } });
此外,您的 count 屬性應該是數字,而不是字串(您會收到 Vue 警告):
modelValue: { type: Number, required: true, default: 0 },
此外,無需更新 input
事件上的 prop,因為您已經在 上使用
v-model
>。您還應該將輸入的模型轉換為數字:
<input type="text" v-model.number="value" :disabled="props.disabled" placeholder="0"/>
所以你有: 應用程式視圖
<template> <p> <ProductCounter v-model="formData.productCount" label="product count" /> </p> <button @click="clearUI"> Clear </button> <div> {{ JSON.stringify(formData) }} </div> </template> <script setup> import ProductCounter from './ProductCounter.vue' import {reactive} from 'vue' const initialFormData = { productCount: 0, firstname: '', surname: '', phone: '', email: '', postcode: '', submittedOnce: false, errors: [] } let formData = reactive({ ...initialFormData }); const clearUI = () => { Object.assign(formData, initialFormData); } </script>
ProductCounter.vue:
<template> <div class="form__row"> <div class="counter__row"> <label>{{ label }}</label> <div class="form__counter"> <button :disabled="value == 0 || props.disabled" @click.prevent="value--"> - </button> <input type="text" v-model.number="value" :disabled="props.disabled" placeholder="0"/> <button :disabled="props.disabled" @click.prevent="value++"> + </button> </div> </div> </div> </template> <script setup> import {computed} from 'vue'; const emits = defineEmits(['update:modelValue']); const props = defineProps({ label: { type: String, required: true }, modelValue: { type: Number, required: true, default: 0 }, disabled: { type: Boolean, required: false } }); const value = computed({ get(){ return props.modelValue; }, set(val){ emits('update:modelValue', val); } }); </script>