欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue3中子組件改變父組件傳過來的值(props)的方法小結

 更新時間:2025年04月01日 09:56:16   作者:匹馬夕陽  
在 Vue 3 中,子組件改變父組件傳過來的值(props)的方法主要有以下幾種:通過事件發(fā)射、使用 v-model、模擬 .sync 修飾符的功能(Vue 3 中已移除),以及使用 ref 或 reactive,下面我將結合代碼示例和使用場景詳細講解這些方法,需要的朋友可以參考下

1. 通過事件發(fā)射

使用場景:當子組件需要顯式通知父組件更新 props 的值時,事件發(fā)射是一種常見且推薦的方式。這種方法清晰地表達了數(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 維護本地狀態(tài),并通過 watch 確保與父組件的 prop 同步。
  • 當輸入框的值改變時,子組件通過 emit('update:message') 通知父組件更新 parentMessage。

2. 使用 v-model

使用場景:當 prop 需要雙向綁定時(例如表單輸入),使用 v-model 是一種簡潔的方式。它本質(zhì)上是對事件發(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 綁定到子組件,實際上是監(jiān)聽 update:message 事件并傳遞 prop。
  • 子組件通過 computed 創(chuàng)建一個計算屬性 localMessage,其 getter 返回 prop 值,setter 觸發(fā) update:message 事件。
  • 輸入框的值改變時,localMessage 的 setter 會被調(diào)用,通知父組件更新 parentMessage。

3. 模擬 .sync 修飾符的功能

使用場景:在 Vue 2 中,.sync 修飾符用于雙向綁定 prop,但在 Vue 3 中被移除??梢酝ㄟ^顯式的事件發(fā)射實現(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。
  • 子組件在按鈕點擊時通過 emit 發(fā)射 update:message 事件,攜帶新值。
  • 這種模式與 Vue 2 的 .sync 類似,但需要手動實現(xiàn)。

4. 使用 ref 或 reactive

使用場景:當 prop 是一個對象或數(shù)組時,可以將它包裝在 ref 或 reactive 中傳遞給子組件。子組件可以直接修改這些值,因為它們是響應式的。這種方式適合復雜數(shù)據(jù)結構的場景,但可能模糊單向數(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)建一個響應式對象 parentData,并將其作為 prop 傳遞給子組件。
  • 子組件直接操作 data.message,由于它是響應式對象,修改會自動反映到父組件。
  • 注意:這種方式繞過了顯式的事件發(fā)射,建議謹慎使用,避免數(shù)據(jù)流向不清晰。

總結

以下是各種方法的適用場景和特點:

  • 事件發(fā)射:最符合 Vue 單向數(shù)據(jù)流原則,適合需要顯式通知父組件的場景。
  • v-model:簡潔優(yōu)雅,適合表單輸入等雙向綁定場景。
  • 模擬 .sync:Vue 3 中替代 .sync 的手動實現(xiàn),適合特定 prop 的更新。
  • ref 或 reactive:適合傳遞對象或數(shù)組,子組件可以直接修改,但需注意數(shù)據(jù)流清晰度。

根據(jù)具體需求選擇合適的方法,確保代碼的可維護性和數(shù)據(jù)流的一致性。

以上就是Vue3中子組件改變父組件傳過來的值(props)的方法和使用場景的詳細內(nèi)容,更多關于Vue3子組件改變父組件傳過來的值的資料請關注腳本之家其它相關文章!

相關文章

  • vue實現(xiàn)盒子內(nèi)拖動方塊移動的示例代碼

    vue實現(xiàn)盒子內(nèi)拖動方塊移動的示例代碼

    這篇文章主要給大家介紹了如何通過vue實現(xiàn)盒子內(nèi)拖動方塊移動,文章通過代碼示例講解的非常詳細,具有一定的參考價值,感興趣的小伙伴可以參考閱讀本文
    2023-08-08
  • Vue js 的生命周期(看了就懂)(推薦)

    Vue js 的生命周期(看了就懂)(推薦)

    這篇文章主要介紹了Vue js生命周期,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03
  • Vue.js 的移動端組件庫mint-ui實現(xiàn)無限滾動加載更多的方法

    Vue.js 的移動端組件庫mint-ui實現(xiàn)無限滾動加載更多的方法

    下面小編就為大家分享一篇Vue.js 的移動端組件庫mint-ui實現(xiàn)無限滾動加載更多的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • 詳解vue2.0腳手架的webpack 配置文件分析

    詳解vue2.0腳手架的webpack 配置文件分析

    本篇文章主要介紹了詳解vue2.0腳手架的webpack 配置文件分析,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • vue3.0 CLI - 2.1 -  component 組件入門教程

    vue3.0 CLI - 2.1 - component 組件入門教程

    這篇文章主要介紹了vue3.0 CLI - 2.1 - component 組件入門教程,本文主要的關注點就是組件,本文通過實例代碼相結合的形式給大家介紹的非常詳細,需要的朋友可以參考下
    2018-09-09
  • 利用vueJs實現(xiàn)圖片輪播實例代碼

    利用vueJs實現(xiàn)圖片輪播實例代碼

    本篇文章主要介紹了利用vueJs實現(xiàn)圖片輪播實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • vue前端編譯報錯的圖文解決方法

    vue前端編譯報錯的圖文解決方法

    Vue框架可以很方便的引入各種插件,但是也因此會經(jīng)常遇到種編譯報錯,這篇文章主要給大家介紹了關于vue前端編譯報錯解決的相關資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2024-03-03
  • Vue實現(xiàn)動態(tài)顯示textarea剩余字數(shù)

    Vue實現(xiàn)動態(tài)顯示textarea剩余字數(shù)

    這篇文章主要為大家詳細介紹了Vue實現(xiàn)動態(tài)顯示textarea剩余文字數(shù)量,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Vue前端登錄token信息驗證功能實現(xiàn)

    Vue前端登錄token信息驗證功能實現(xiàn)

    最近公司新啟動了個項目,用的是vue框架在做,下面這篇文章主要給大家介紹了關于vue實現(xiàn)token登錄驗證的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-12-12
  • Javascript對象及Proxy工作原理詳解

    Javascript對象及Proxy工作原理詳解

    這篇文章主要為大家介紹了Javascript對象及Proxy工作原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08

最新評論