Vue3?組件之間傳值及適用場景分析
在 Vue3 中,組件之間的數(shù)據(jù)傳遞主要有以下幾種方式,適用于不同的場景:
一、父組件向子組件傳值:props
1. 子組件定義 props
<!-- ChildComponent.vue -->
<script setup>
// 組合式 API(推薦)
const props = defineProps({
title: {
type: String,
default: '默認(rèn)標(biāo)題'
},
count: {
type: Number,
required: true
}
});
</script>
<template>
<div>{{ title }} - {{ count }}</div>
</template><!-- 選項(xiàng)式 API -->
<script>
export default {
props: {
title: String,
count: {
type: Number,
required: true
}
}
}
</script>2. 父組件傳遞數(shù)據(jù)
<template>
<ChildComponent
:title="'Hello Vue3'"
:count="42"
/>
</template>二、子組件向父組件傳值:emit 事件
1. 子組件觸發(fā)事件
<!-- ChildComponent.vue -->
<script setup>
const emit = defineEmits(['updateCount']); // 定義事件
function increment() {
emit('updateCount', 10); // 觸發(fā)事件并傳遞數(shù)據(jù)
}
</script>
<template>
<button @click="increment">增加計(jì)數(shù)</button>
</template>2. 父組件監(jiān)聽事件
<template>
<ChildComponent
:count="count"
@updateCount="count = $event" <!-- 接收子組件傳遞的值 -->
/>
</template>
<script setup>
import { ref } from 'vue';
const count = ref(0);
</script>三、雙向綁定:v-model
1. 子組件支持 v-model
<!-- ChildComponent.vue -->
<script setup>
const props = defineProps(['modelValue']);
const emit = defineEmits(['update:modelValue']);
function updateValue(newValue) {
emit('update:modelValue', newValue);
}
</script>
<template>
<input
:value="modelValue"
@input="updateValue($event.target.value)"
>
</template>2. 父組件使用 v-model
<template>
<ChildComponent v-model="text" /> <!-- 自動同步數(shù)據(jù) -->
</template>
<script setup>
import { ref } from 'vue';
const text = ref('初始值');
</script>四、跨層級傳值:provide / inject
1. 祖先組件提供數(shù)據(jù)
<!-- AncestorComponent.vue -->
<script setup>
import { provide, ref } from 'vue';
const theme = ref('dark');
provide('theme', theme); // 提供數(shù)據(jù)
</script>2. 后代組件注入數(shù)據(jù)
<!-- ChildComponent.vue -->
<script setup>
import { inject } from 'vue';
const theme = inject('theme'); // 注入數(shù)據(jù)
</script>
<template>
<div :class="theme">當(dāng)前主題:{{ theme }}</div>
</template>五、通過 Context 共享數(shù)據(jù)(如 Pinia 狀態(tài)管理)
1. 安裝 Pinia
npm install pinia
2. 創(chuàng)建 Store
// stores/counter.js
import { defineStore } from 'pinia';
export const useCounterStore = defineStore('counter', {
state: () => ({
count: 0
}),
actions: {
increment() {
this.count++;
}
}
});3. 組件中使用 Store
<template>
<div>Count: {{ counterStore.count }}</div>
<button @click="counterStore.increment()">+1</button>
</template>
<script setup>
import { useCounterStore } from '@/stores/counter';
const counterStore = useCounterStore();
</script>總結(jié):不同場景的傳值方式
| 場景 | 方法 | 適用性 |
|---|---|---|
| 父子組件直接通信 | props + emit | 父子層級明確,數(shù)據(jù)流單向 |
| 表單輸入雙向綁定 | v-model | 表單類組件(如輸入框、下拉框) |
| 跨層級組件通信 | provide / inject | 深層次嵌套組件(如全局配置、主題) |
| 復(fù)雜應(yīng)用狀態(tài)共享 | Pinia / Vuex | 多組件共享狀態(tài)(推薦中大型項(xiàng)目) |
推薦實(shí)踐:
- 優(yōu)先使用
props和emit實(shí)現(xiàn)父子通信。 - 簡單雙向綁定用
v-model,復(fù)雜邏輯用事件。 - 跨層級或全局狀態(tài)使用 Pinia。
- 避免過度依賴
provide/inject,可能導(dǎo)致組件耦合。
到此這篇關(guān)于Vue3 組件之間傳值的文章就介紹到這了,更多相關(guān)Vue3 組件之間傳值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue結(jié)合Openlayers使用Overlay添加Popup彈窗實(shí)現(xiàn)
本文主要介紹了Vue結(jié)合Openlayers使用Overlay添加Popup彈窗實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
webpack 3 + Vue2 使用dotenv配置多環(huán)境的步驟
這篇文章主要介紹了webpack 3 + Vue2 使用dotenv配置多環(huán)境,env文件在配置文件都可以用, vue頁面用的時(shí)候需要在 webpack.base.conf.js 重新配置,需要的朋友可以參考下2023-11-11
Vue3組件掛載之創(chuàng)建組件實(shí)例詳解
這篇文章主要為大家介紹了Vue3組件掛載之創(chuàng)建組件實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Vue項(xiàng)目字體文件打包后fonts文件夾消失的原因分析與解決方案
在 Vue 項(xiàng)目中,將字體文件放在 src/assets/fonts 目錄下,打包后在瀏覽器中訪問發(fā)現(xiàn)路徑里不見了 fonts文件夾,這通常不是文件丟失,而是 Vue 的構(gòu)建工具對靜態(tài)資源進(jìn)行了自動化處理,本文給大家介紹了Vue項(xiàng)目字體文件打包后fonts文件夾消失的原因分析與解決方案2026-06-06
vue實(shí)現(xiàn)商城秒殺倒計(jì)時(shí)功能
這篇文章主要介紹了vue實(shí)現(xiàn)商城秒殺倒計(jì)時(shí)功能,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12

