Vue3中子組件改變父組件傳過來的值(props)的方法小結(jié)
1. 通過事件發(fā)射
使用場景:當(dāng)子組件需要顯式通知父組件更新 props 的值時(shí),事件發(fā)射是一種常見且推薦的方式。這種方法清晰地表達(dá)了數(shù)據(jù)流向,符合 Vue 的單向數(shù)據(jù)流原則。
代碼示例:
// 父組件
<template>
<div>
<Child :message="parentMessage" @update:message="updateMessage" />
<p>父組件值:{{ parentMessage }}</p>
</div>
</template>
<script setup>
import { ref } from 'vue';
import Child from './Child.vue';
const parentMessage = ref('Hello from parent');
const updateMessage = (newMessage) => {
parentMessage.value = newMessage;
};
</script>
// 子組件
<template>
<div>
<input v-model="localMessage" @input="handleInput" />
</div>
</template>
<script setup>
import { ref, watch } from 'vue';
const props = defineProps({
message: String
});
const emit = defineEmits(['update:message']);
const localMessage = ref(props.message);
// 監(jiān)聽 props 的變化,同步本地值
watch(() => props.message, (newVal) => {
localMessage.value = newVal;
});
const handleInput = () => {
emit('update:message', localMessage.value);
};
</script>
解釋:
- 父組件將
parentMessage作為 prop 傳遞給子組件,并監(jiān)聽子組件發(fā)出的update:message事件。 - 子組件使用
localMessage維護(hù)本地狀態(tài),并通過watch確保與父組件的 prop 同步。 - 當(dāng)輸入框的值改變時(shí),子組件通過
emit('update:message')通知父組件更新parentMessage。
2. 使用 v-model
使用場景:當(dāng) prop 需要雙向綁定時(shí)(例如表單輸入),使用 v-model 是一種簡潔的方式。它本質(zhì)上是對(duì)事件發(fā)射的封裝,適合需要頻繁更新的場景。
代碼示例:
// 父組件
<template>
<div>
<Child v-model:message="parentMessage" />
<p>父組件值:{{ parentMessage }}</p>
</div>
</template>
<script setup>
import { ref } from 'vue';
import Child from './Child.vue';
const parentMessage = ref('Hello from parent');
</script>
// 子組件
<template>
<div>
<input v-model="localMessage" />
</div>
</template>
<script setup>
import { computed } from 'vue';
const props = defineProps({
message: String
});
const emit = defineEmits(['update:message']);
const localMessage = computed({
get() {
return props.message;
},
set(value) {
emit('update:message', value);
}
});
</script>
解釋:
- 父組件使用
v-model:message將parentMessage綁定到子組件,實(shí)際上是監(jiān)聽update:message事件并傳遞 prop。 - 子組件通過
computed創(chuàng)建一個(gè)計(jì)算屬性localMessage,其 getter 返回 prop 值,setter 觸發(fā)update:message事件。 - 輸入框的值改變時(shí),
localMessage的 setter 會(huì)被調(diào)用,通知父組件更新parentMessage。
3. 模擬 .sync 修飾符的功能
使用場景:在 Vue 2 中,.sync 修飾符用于雙向綁定 prop,但在 Vue 3 中被移除??梢酝ㄟ^顯式的事件發(fā)射實(shí)現(xiàn)類似功能,適合需要更新特定 prop 的場景。
代碼示例:
// 父組件
<template>
<div>
<Child :message="parentMessage" @update:message="parentMessage = $event" />
<p>父組件值:{{ parentMessage }}</p>
</div>
</template>
<script setup>
import { ref } from 'vue';
import Child from './Child.vue';
const parentMessage = ref('Hello from parent');
</script>
// 子組件
<template>
<div>
<button @click="updateMessage">更新消息</button>
</div>
</template>
<script setup>
const props = defineProps({
message: String
});
const emit = defineEmits(['update:message']);
const updateMessage = () => {
emit('update:message', 'New message from child');
};
</script>
解釋:
- 父組件監(jiān)聽
update:message事件,并直接將事件參數(shù)賦值給parentMessage。 - 子組件在按鈕點(diǎn)擊時(shí)通過
emit發(fā)射update:message事件,攜帶新值。 - 這種模式與 Vue 2 的
.sync類似,但需要手動(dòng)實(shí)現(xiàn)。
4. 使用 ref 或 reactive
使用場景:當(dāng) prop 是一個(gè)對(duì)象或數(shù)組時(shí),可以將它包裝在 ref 或 reactive 中傳遞給子組件。子組件可以直接修改這些值,因?yàn)樗鼈兪琼憫?yīng)式的。這種方式適合復(fù)雜數(shù)據(jù)結(jié)構(gòu)的場景,但可能模糊單向數(shù)據(jù)流的邊界。
代碼示例:
// 父組件
<template>
<div>
<Child :data="parentData" />
<p>父組件值:{{ parentData.message }}</p>
</div>
</template>
<script setup>
import { reactive } from 'vue';
import Child from './Child.vue';
const parentData = reactive({ message: 'Hello from parent' });
</script>
// 子組件
<template>
<div>
<input v-model="data.message" />
</div>
</template>
<script setup>
const props = defineProps({
data: Object
});
</script>
解釋:
- 父組件使用
reactive創(chuàng)建一個(gè)響應(yīng)式對(duì)象parentData,并將其作為 prop 傳遞給子組件。 - 子組件直接操作
data.message,由于它是響應(yīng)式對(duì)象,修改會(huì)自動(dòng)反映到父組件。 - 注意:這種方式繞過了顯式的事件發(fā)射,建議謹(jǐn)慎使用,避免數(shù)據(jù)流向不清晰。
總結(jié)
以下是各種方法的適用場景和特點(diǎn):
- 事件發(fā)射:最符合 Vue 單向數(shù)據(jù)流原則,適合需要顯式通知父組件的場景。
v-model:簡潔優(yōu)雅,適合表單輸入等雙向綁定場景。- 模擬
.sync:Vue 3 中替代.sync的手動(dòng)實(shí)現(xiàn),適合特定 prop 的更新。 ref或reactive:適合傳遞對(duì)象或數(shù)組,子組件可以直接修改,但需注意數(shù)據(jù)流清晰度。
根據(jù)具體需求選擇合適的方法,確保代碼的可維護(hù)性和數(shù)據(jù)流的一致性。
以上就是Vue3中子組件改變父組件傳過來的值(props)的方法和使用場景的詳細(xì)內(nèi)容,更多關(guān)于Vue3子組件改變父組件傳過來的值的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue-resource?獲取本地json數(shù)據(jù)404問題的解決
這篇文章主要介紹了vue-resource?獲取本地json數(shù)據(jù)404問題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Vue.js與 ASP.NET Core 服務(wù)端渲染功能整合
本文通過案例給大家詳細(xì)分析了ASP.NET Core 與 Vue.js 服務(wù)端渲染,需要的朋友可以參考下2017-11-11
基于Vue3實(shí)現(xiàn)數(shù)字華容道游戲的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Vue編寫一個(gè)數(shù)字華容道游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
vue 添加和編輯用同一個(gè)表單,el-form表單提交后清空表單數(shù)據(jù)操作
這篇文章主要介紹了vue 添加和編輯用同一個(gè)表單,el-form表單提交后清空表單數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
解決VMware中vmware-vmx.exe進(jìn)程無法關(guān)閉以及死機(jī)等問題
這篇文章主要介紹了解決VMware中vmware-vmx.exe進(jìn)程無法關(guān)閉以及死機(jī)等問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
vue+node 實(shí)現(xiàn)視頻在線播放的實(shí)例代碼
這篇文章主要介紹了vue+node 實(shí)現(xiàn)視頻在線播放的實(shí)例代碼,代碼簡單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Vue.js使用computed屬性實(shí)現(xiàn)數(shù)據(jù)自動(dòng)更新
在Vue組件中,computed屬性是在組件的選項(xiàng)對(duì)象中聲明的,你可以把它們想象成組件的一個(gè)小功能,告訴Vue當(dāng)某些數(shù)據(jù)變化時(shí),如何更新界面,本文給大家介紹了Vue.js使用computed屬性實(shí)現(xiàn)數(shù)據(jù)自動(dòng)更新,需要的朋友可以參考下2024-06-06

