Vue3中各種靈活傳遞數(shù)據(jù)的方式小結(jié)
前言、理解Vue 3的數(shù)據(jù)流
Vue 3 提供了多種數(shù)據(jù)傳遞的方式,讓我們的組件之間可以盡情地交流。接下來,我們就直接一個個來看,這些方式都是怎么工作的。
一、Props:從父到子的經(jīng)典路徑
在 Vue 中,子組件可以通過 props 接收父組件傳遞過來的數(shù)據(jù),這可是從父組件到子組件傳遞數(shù)據(jù)的老傳統(tǒng)了。這就像家長給孩子零花錢一樣,孩子只能使用家長給的,不能自己去拿家里的錢。
// 父組件
<template>
<ChildComponent :message="greeting" />
</template>
<script setup>
import ChildComponent from './ChildComponent.vue';
const greeting = 'Hello!';
</script>
// 子組件
<script setup>
const props = defineProps({
message: String,
});
console.log(message); // "Hello!"
</script>
<script setup> 使用了 Composition API,它簡化了 prop 的訪問方式。所以我們不需要通過 props 對象來訪問 prop 的值,可以直接將 prop 聲明為一個變量,并在模板或邏輯中使用它。
二、Emits:子組件的呼喚
如果子組件需要某些數(shù)據(jù)時呢,總不能坐等著父組件傳遞吧。所以接著是Emits,它讓子組件能夠向上發(fā)射信號。通過defineEmits和$emit,子組件可以告訴父組件:“我有事情要通知!”
// 父組件
<template>
<ChildComponent @updateMessage="newGreeting = $event" />
<p>{{ newGreeting }}</p>
</template>
<script setup>
import ChildComponent from './ChildComponent.vue';
let newGreeting = '';
</script>
// 子組件
<script setup>
const emit = defineEmits(['updateMessage']);
const updateParent = () => {
emit('updateMessage', 'Updated from child!');
};
</script>
父組件包含一個子組件實例,并監(jiān)聽子組件發(fā)出的 updateMessage 事件。當這個事件被觸發(fā)時,父組件會更新其內(nèi)部的 newGreeting 數(shù)據(jù)屬性。
子組件中定義了一個方法 updateParent,當調(diào)用這個方法時,它會觸發(fā) updateMessage 事件,并傳遞一個字符串作為參數(shù)給父組件。
每當觸發(fā) updateMessage 事件,父組件會接收到這個事件并更新 newGreeting 的值,從而在頁面上顯示更新后的消息。
這里,
$event只是一個約定俗成的變量名,用于在事件處理器中捕獲事件對象或者事件傳遞的數(shù)據(jù)。通常為了使代碼更易于閱讀和理解,我們會根據(jù)事件數(shù)據(jù)的實際內(nèi)容來選擇合適的變量名。
三、provide / inject:上下文中的通信
有時候組件會嵌套的比較深,如父組件中有子組件,子組件中有孫組件……當組件層級很深,或者需要跨多個組件共享數(shù)據(jù)時,provide 和 inject 就派上用場了。它們能讓我們在組件樹中自由傳遞數(shù)據(jù),而不必層層傳遞props。這在處理深層次嵌套的組件時非常有用。
// 父組件
<script setup>
import { provide } from 'vue';
provide('theme', 'dark');
</script>
// 子孫組件
<script setup>
import { inject } from 'vue';
const theme = inject('theme');
console.log(theme); // "dark"
</script>
在父組件中,我們使用 provide 函數(shù)來提供一個名為 'theme' 的鍵以及對應(yīng)的值 'dark'。這使得 'theme' 可以被其所有子組件及其子代組件訪問。
在子孫組件中,你使用 inject 函數(shù)來獲取 'theme' 鍵的值。inject 函數(shù)會查找最近的祖先組件中提供的與 'theme' 鍵相匹配的值。
當 inject 被調(diào)用時,Vue 會沿著組件樹向上查找,直到找到一個與注入鍵匹配的提供者。如果找不到匹配的提供者,inject 將返回 undefined。為了避免這種情況,可以為 inject 提供一個默認值。
const theme = inject('theme', 'light'); // 如果沒有找到提供者,則使用 'light' 作為默認值
雖然 provide / inject 避免了必須在每一層組件之間傳遞 props 的繁瑣,但過度使用這種方法可能導致組件之間的耦合度增加,使得代碼難以追蹤和維護,所以建議適當使用。
四、Vuex:全局狀態(tài)管理的專家
既然 provide / inject 不好過度使用,那么現(xiàn)在就講到 Vuex,它是處理復雜應(yīng)用中全局狀態(tài)的理想選擇。通過Vuex,我們可以輕松地管理跨組件的狀態(tài)。
1、安裝 Vuex
如果還沒有安裝 Vuex,就直接 npm。
npm install vuex
2、創(chuàng)建 Vuex Store
首先,需要創(chuàng)建一個 Vuex store 文件。
import { createStore } from 'vuex';
export default createStore({
state: {
theme: 'light',
},
mutations: {
setTheme(state, theme) {
state.theme = theme;
},
},
actions: {
changeTheme({ commit }, theme) {
commit('setTheme', theme);
},
},
getters: {
currentTheme(state) {
return state.theme;
},
},
});
state是 store 的數(shù)據(jù)容器。mutations是提交到 store 的唯一方式,它們負責修改 state。actions是異步操作的處理函數(shù),可以包含任意的異步操作邏輯。getters是 store 的計算屬性,用于從 store 中獲取狀態(tài)的派生狀態(tài)。
3、在主應(yīng)用文件中使用 Vuex Store
在我們的主應(yīng)用文件中 main.js 中,引入并使用 Vuex store。
import { createApp } from 'vue';
import App from './App.vue';
import store from './store';
const app = createApp(App);
app.use(store);
app.mount('#app');
4、在組件中使用 Vuex Store
直接使用 store 的屬性和方法。
<template>
<div :class="theme">
<!-- 使用 getter -->
<p>The current theme is {{ currentTheme }}.</p>
<!-- 使用 action -->
<button @click="changeTheme('dark')">Switch to dark theme</button>
</div>
</template>
<script setup>
import { computed, onMounted } from 'vue';
import { useStore } from 'vuex';
const store = useStore();
const theme = computed(() => store.state.theme);
const currentTheme = computed(() => store.getters.currentTheme);
function changeTheme(newTheme) {
store.dispatch('changeTheme', newTheme);
}
onMounted(() => {
console.log('Current theme:', store.getters.currentTheme);
});
</script>
theme和currentTheme是使用computed創(chuàng)建的響應(yīng)式引用,分別映射到 store 的狀態(tài)和 getter。changeTheme函數(shù)使用store.dispatch來觸發(fā)changeThemeaction,參數(shù)是新的主題值。onMounted鉤子確保在組件掛載后立即執(zhí)行,用于檢查并記錄當前的主題。
當主題在 Vuex store 中發(fā)生變化時,由于 theme 和 currentTheme 的響應(yīng)式特性,組件會自動更新 UI 以反映新的主題。changeTheme 函數(shù)提供了切換主題的功能,可以通過點擊按鈕來觸發(fā)主題的改變。
除了 vuex ,還有一個 pinia 和它相似,這里就不介紹了。
結(jié)語
到這里,我們一同探索了Vue 3中豐富多彩的數(shù)據(jù)傳遞方式,從基礎(chǔ)的Props和Emits,到進階的Provide與Inject,以及全局狀態(tài)管理的Vuex。每種方法都有其獨特的應(yīng)用場景,就看我們怎么選擇了。
以上就是Vue3中各種靈活傳遞數(shù)據(jù)的方式小結(jié)的詳細內(nèi)容,更多關(guān)于Vue3傳遞數(shù)據(jù)方式的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue.js學習記錄之在元素與template中使用v-if指令實例
這篇文章主要給大家介紹了關(guān)于Vue.js學習記錄之在元素與template中使用v-if指令的相關(guān)資料,文中給出了詳細的示例代碼供大家參考學習,相信對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-06-06
vue 獲取到數(shù)據(jù)但卻渲染不到頁面上的解決方法
這篇文章主要介紹了vue 獲取到數(shù)據(jù)但卻渲染不到頁面上的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11
vue+elementUI實現(xiàn)動態(tài)面包屑
這篇文章主要為大家詳細介紹了vue+elementUI實現(xiàn)動態(tài)面包屑,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04

