亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue3組件傳參的多種方式小結(jié)

 更新時間:2025年01月14日 08:40:03   作者:五號廠房  
這篇文章主要介紹 Vue3組件傳參的方式,包括父子組件、跨層級組件、兄弟組件的傳參,如父子組件通過屬性綁定和自定義事件雙向傳參,跨層級組件可利用 Provide/Inject 機制和 Pinia 狀態(tài)管理,兄弟組件可通過父組件中轉(zhuǎn)或 mitt 庫直接傳參,需要的朋友可以參考下

一、Vue3 組件傳參概述

在 Vue 3 的應(yīng)用開發(fā)旅程中,組件間的高效通信是構(gòu)建復雜交互式應(yīng)用的關(guān)鍵基石。無論是小型項目中簡單的父子組件協(xié)作,還是大型應(yīng)用里多層嵌套組件、兄弟組件之間的默契配合,精準且流暢的參數(shù)傳遞都起著決定性作用。它確保了數(shù)據(jù)能在各個組件 “崗位” 按需流動,使得整個應(yīng)用如同精密運轉(zhuǎn)的機器,為用戶帶來流暢且一致的體驗。接下來,就讓我們深入探究 Vue 3 組件傳參的奧秘世界。

二、父子組件傳參

(一)父組件向子組件傳參

父組件作為數(shù)據(jù)的源頭,常常需要將某些信息精準地傳遞給子組件,以便子組件基于這些數(shù)據(jù)進行個性化展示或執(zhí)行特定邏輯。在 Vue 3 中,這一過程通過屬性綁定優(yōu)雅實現(xiàn)。例如,父組件擁有一個關(guān)鍵數(shù)據(jù)parentMessage,在使用子組件標簽時,像這樣綁定數(shù)據(jù):

<template>
  <ChildComponent :message="parentMessage" />
</template>
<script setup>
import ChildComponent from './ChildComponent.vue';
const parentMessage = '這是來自父組件的消息';
</script>

而子組件則利用defineProps來接收這份來自父方的 “饋贈”:

<template>
  <div>{{ message }}</div>
</template>
<script setup>
import { defineProps } from 'vue';
const props = defineProps({
  message: String
});
</script>

如此一來,子組件就能順利獲取父組件傳遞的message數(shù)據(jù),并在模板中靈活運用。

(二)子組件向父組件傳參

有時,子組件內(nèi)部發(fā)生的某些變化或產(chǎn)生的數(shù)據(jù),需要及時反饋給父組件知曉。這時,子組件就會扮演信息傳遞者的角色,通過defineEmits觸發(fā)自定義事件來達成這一目的。假設(shè)子組件中有個按鈕,點擊后要向父組件傳遞一段文本數(shù)據(jù),代碼如下:

<template>
  <button @click="sendDataToParent">向父組件傳數(shù)據(jù)</button>
</template>
<script setup>
import { defineEmits } from 'vue';
const emits = defineEmits(['send-data']);
const sendDataToParent = () => {
  const data = '子組件的數(shù)據(jù)';
  emits('send-data', data);
};
</script>

父組件則在使用子組件標簽時,敏銳地監(jiān)聽這個自定義事件:

<template>
  <ChildComponent @send-data="handleChildData" />
</template>
<script setup>
import ChildComponent from './ChildComponent.vue';
const handleChildData = (data) => {
  console.log('收到子組件數(shù)據(jù):', data);
};
</script>

這般雙向的信息互通,讓父子組件間的協(xié)作親密無間。

(三)案例實操:計數(shù)器組件

為了更直觀地感受父子組件傳參的魅力,我們構(gòu)建一個簡易計數(shù)器案例。父組件負責展示計數(shù)器的當前數(shù)值,并提供操作按鈕,子組件則承載計數(shù)器的增減邏輯。
父組件:

<template>
  <div>
    當前計數(shù): <span>{{ count }}</span>
    <ChildComponent :count="count" @update-count="updateCount" />
  </div>
</template>
<script setup>
import ChildComponent from './ChildComponent.vue';
import { ref } from 'vue';
const count = ref(0);
const updateCount = (newCount) => {
  count.value = newCount;
};
</script>
<template>
  <button @click="increment">增加</button>
  <button @click="decrement">減少</button>
</template>
<script setup>
import { defineProps, defineEmits } from 'vue';
const props = defineProps({
  count: Number
});
const emits = defineEmits(['update:count']);
const increment = () => {
  emits('update:count', props.count + 1);
};
const decrement = () => {
  emits('update:count', props.count - 1);
};
</script>

當點擊子組件中的增減按鈕,子組件通過事件將新的計數(shù)數(shù)值傳遞給父組件,父組件更新自身狀態(tài),實現(xiàn)了計數(shù)器功能的流暢運作。

三、跨層級組件傳參

(一)Provide/Inject 機制

在多層嵌套的組件架構(gòu)中,若按照常規(guī)的父子組件傳參方式一級級傳遞數(shù)據(jù),會使代碼變得繁瑣不堪,猶如一條冗長且錯綜復雜的信息傳遞鏈。此時,Vue 3 提供的 Provide/Inject 機制就如同一條 “捷徑”,讓祖先組件能夠直接為后代組件 “空投” 所需數(shù)據(jù)。
祖先組件:

<template>
  <div>
    <MiddleComponent />
  </div>
</template>
<script setup>
import { provide } from 'vue';
import MiddleComponent from './MiddleComponent.vue';
const sharedData = '祖先共享的數(shù)據(jù)';
provide('sharedData', sharedData);
</script>

中間組件(僅作為層級過渡,不做數(shù)據(jù)處理):

<template>
  <div>
    <ChildComponent />
  </div>
</template>
<script setup>
import ChildComponent from './ChildComponent.vue';
</script>

后代組件:

<template>
  <div>{{ injectedData }}</div>
</template>
<script setup>
import { inject } from 'vue';
const injectedData = inject('sharedData');
</script>

通過provide在祖先組件聲明共享數(shù)據(jù),后代組件利用inject輕松獲取,大大簡化了跨層級數(shù)據(jù)傳遞的復雜度。

(二)Pinia 狀態(tài)管理

Pinia 作為 Vue 3 生態(tài)中備受矚目的狀態(tài)管理庫,為跨層級組件數(shù)據(jù)共享提供了一套強大且優(yōu)雅的解決方案。相較于傳統(tǒng)的 Vuex,它具有更簡潔的 API 和更直觀的使用方式。
首先,創(chuàng)建一個 Pinia store 實例:

import { defineStore } from 'pinia';

export const useCounterStore = defineStore('counter', {
  state: () => ({
    count: 0
  }),
  actions: {
    increment() {
      this.count++;
    },
    decrement() {
      this.count--;
    }
  }
});

在組件中使用時,無論是哪個層級的組件,只要引入并調(diào)用相應(yīng)方法,就能實現(xiàn)數(shù)據(jù)的共享與操作:
父組件:

<template>
  <div>
    當前計數(shù): <span>{{ counterStore.count }}</span>
    <ChildComponent />
  </div>
</template>
<script setup>
import { useCounterStore } from './store';
const counterStore = useCounterStore();
</script>

子組件:

<template>
  <button @click="increment">增加</button>
  <button @click="decrement">減少</button>
</template>
<script setup>
import { useCounterStore } from './store';
const counterStore = useCounterStore();
const increment = () => {
  counterStore.increment();
};
const decrement = () => {
  counterStore.decrement();
};
</script>

通過 Pinia,組件們仿佛連接到了一個共享的數(shù)據(jù)中樞,輕松實現(xiàn)跨層級的無縫協(xié)作。

四、兄弟組件傳參

(一)通過父組件中轉(zhuǎn)

當兄弟組件需要交流信息時,由于它們在層級上平起平坐,無法直接傳遞數(shù)據(jù),這時父組件就可以充當一個可靠的 “中間人”。例如,有兩個兄弟組件,一個是輸入框組件用于輸入文本,另一個是顯示組件用于展示輸入框中的內(nèi)容。
父組件:

<template>
  <InputComponent @input-text="handleInputText" />
  <DisplayComponent :display-text="displayText" />
</template>
<script setup>
import InputComponent from './InputComponent.vue';
import DisplayComponent from './DisplayComponent.vue';
import { ref } from 'vue';
const displayText = ref('');
const handleInputText = (text) => {
  displayText.value = text;
};
</script>

輸入框組件:

<template>
  <input type="text" @input="sendText" />
</template>
<script setup>
import { defineEmits } from 'vue';
const emits = defineEmits(['input-text']);
const sendText = (e) => {
  emits('input-text', e.target.value);
};
</script>

顯示組件:

<template>
  <div>{{ displayText }}</div>
</template>
<script setup>
import { defineProps } from 'vue';
const props = defineProps({
  displayText: String
});
</script>

輸入框組件將輸入的文本通過父組件中轉(zhuǎn)傳遞給顯示組件,實現(xiàn)了兄弟組件間的間接通信。

(二)使用 mitt 庫直接傳參

除了借助父組件中轉(zhuǎn),還可以利用 mitt 庫實現(xiàn)兄弟組件間更為直接的參數(shù)傳遞。mitt 庫提供了一個輕量級的事件總線功能。
首先安裝并引入 mitt 庫:

npm install mitt
import mitt from 'mitt';
const emitter = mitt();

在發(fā)送數(shù)據(jù)的組件中:

<template>
  <button @click="sendData">向兄弟組件傳數(shù)據(jù)</button>
</template>
<script setup>
import { ref } from 'vue';
const data = ref('兄弟組件的數(shù)據(jù)');
const sendData = () => {
  emitter.emit('data-transfer', data.value);
};
</script>

在接收數(shù)據(jù)的組件中:

<template>
  <div>{{ receivedData }}</div>
</template>
<script setup>
import { onMounted } from 'vue';
import mitt from './mitt';
const receivedData = ref('');
onMounted(() => {
  mitt.on('data-transfer', (data) => {
    receivedData.value = data;
  });
});
</script>

借助 mitt 庫創(chuàng)建的事件總線,兄弟組件可以跳過父組件,直接建立高效的數(shù)據(jù)傳輸通道。

五、組件傳參的注意事項

(一)單向數(shù)據(jù)流原則

在 Vue 3 組件傳參體系中,單向數(shù)據(jù)流是一條神圣不可侵犯的規(guī)則。父組件傳遞給子組件的數(shù)據(jù),子組件應(yīng)視作只讀的 “圣經(jīng)”,絕不能隨意篡改。若子組件需要基于父組件數(shù)據(jù)進行變更,務(wù)必通過觸發(fā)事件告知父組件,由父組件來完成數(shù)據(jù)的更新操作。這就如同一個嚴謹?shù)闹笓]鏈條,確保數(shù)據(jù)流向的可預測性,避免因隨意修改數(shù)據(jù)引發(fā)難以排查的錯誤,讓整個應(yīng)用始終處于穩(wěn)定可控的狀態(tài)。

(二)屬性命名規(guī)則

組件間傳遞參數(shù)時,屬性命名猶如給數(shù)據(jù)貼上的標簽,需格外謹慎。在父組件的組件標簽上綁定屬性時,要遵循合法的 HTML 屬性命名規(guī)范,避免使用特殊字符或保留字。子組件接收屬性時,命名應(yīng)與父組件傳遞時保持嚴格一致,否則就會出現(xiàn)數(shù)據(jù) “迷路” 的情況,導致組件無法正確獲取所需信息,影響應(yīng)用的正常運行。

六、總結(jié)與展望

回顧 Vue 3 組件傳參的多種精妙方式,從父子組件的親密互動,到跨層級組件的高效協(xié)作,再到兄弟組件的靈活通信,每一種方式都針對特定場景發(fā)揮著獨特優(yōu)勢。這些傳參技巧如同搭建 Vue 3 應(yīng)用大廈的堅固榫卯,緊密連接各個組件模塊,讓應(yīng)用得以穩(wěn)固構(gòu)建并流暢運行。

以上就是Vue3組件傳參的多種方式小結(jié)的詳細內(nèi)容,更多關(guān)于Vue3組件傳參的資料請關(guān)注腳本之家其它相關(guān)文章!

  • vue2之jessibuca視頻插件使用教程詳細講解

    vue2之jessibuca視頻插件使用教程詳細講解

    Jessibuca進行直播流播放,為用戶帶來便捷、高效的視頻觀看體驗,下面這篇文章主要給大家介紹了關(guān)于vue2之jessibuca視頻插件使用的相關(guān)資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2024-09-09
  • Vue.js中v-model指令的用法介紹

    Vue.js中v-model指令的用法介紹

    這篇文章介紹了Vue.js中v-model指令的用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • 使用Vue3實現(xiàn)羊了個羊的算法

    使用Vue3實現(xiàn)羊了個羊的算法

    這篇文章主要介紹了使用Vue3實現(xiàn)羊了個羊的算法,初始化的隨機位置算法,通過實例代碼介紹了計算偏移量的方法,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-09-09
  • vue實現(xiàn)頁面滾動到底部刷新

    vue實現(xiàn)頁面滾動到底部刷新

    這篇文章主要為大家詳細介紹了vue實現(xiàn)頁面滾動到底部刷新,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • vue獲取當前點擊的元素并傳值的實例

    vue獲取當前點擊的元素并傳值的實例

    下面小編就為大家分享一篇vue獲取當前點擊的元素并傳值的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • 使用vue-router beforEach實現(xiàn)判斷用戶登錄跳轉(zhuǎn)路由篩選功能

    使用vue-router beforEach實現(xiàn)判斷用戶登錄跳轉(zhuǎn)路由篩選功能

    這篇文章主要介紹了vue使用vue-router beforEach實現(xiàn)判斷用戶登錄跳轉(zhuǎn)路由篩選,本篇文章默認您已經(jīng)會使用 webpack 或者 vue-cli 來進行環(huán)境的搭建,并且具有一定的vue基礎(chǔ)。需要的朋友可以參考下
    2018-06-06
  • Vue ECharts直角坐標系配置詳細講解

    Vue ECharts直角坐標系配置詳細講解

    數(shù)據(jù)的重要性我們大家都知道,就算再小的項目中都可能使用幾個圖表展示,我最近在做項目的過程中也是需要用到圖表,最后選擇了echarts圖表庫
    2022-12-12
  • vue 基于abstract 路由模式 實現(xiàn)頁面內(nèi)嵌的示例代碼

    vue 基于abstract 路由模式 實現(xiàn)頁面內(nèi)嵌的示例代碼

    這篇文章主要介紹了vue 基于abstract 路由模式 實現(xiàn)頁面內(nèi)嵌的示例代碼,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2020-12-12
  • vue中使用element組件時事件想要傳遞其他參數(shù)的問題

    vue中使用element組件時事件想要傳遞其他參數(shù)的問題

    這篇文章主要介紹了vue中使用element組件時事件想要傳遞其他參數(shù)的問題,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • 最新評論