Vue組件之間四種通信方式詳解
前言
vue 框架提供了前端開發(fā)組件的思想,可以通過組件來組合成一個完整的頁面,都是隨著組件數(shù)量原來越多,組件之間難免需要相互通信,那么如何實現(xiàn)組件之間的通信呢?下面介紹 vue 組件通信的幾種方法
父子組件通信?
父組件傳遞 props
給子組件(數(shù)據(jù)以及改變數(shù)據(jù)的方法),子組件通過 $emit
來更新父組件的狀態(tài)
父組件定義,聲明狀態(tài) { name: 'baidu.com' }
以及可以改變狀態(tài)的方法update()
,通過 name 傳遞和 @update 把 name 屬性和 update 方法傳遞給子組件
<template> <div> <Child : @update="update" /> </div> </template> <script> import Child from "./components/Child"; export default { name: "App", components: { Child, }, data() { return { name: "baidu.com", }; }, methods: { update() { this.name = "www.baidu.com"; }, }, }; </script>vue
子組件的定義,定義 props 接收 name 屬性,通過 $emit 傳遞 update 參數(shù)通知父組件更新狀態(tài)
<template> <div> {{ name }} <button @click="$emit('update')">通知父組件數(shù)據(jù)</button> </div> </template> <script> export default { name: "Child", props: { name: String, }, }; </script>
父組件與子孫組件的通信?
父組件通過 provide 創(chuàng)建對象,子組件通過 inject 來使用父組件的數(shù)據(jù),不受組件層級的限制
父組件通過 provide 定義需要傳遞是數(shù)據(jù)
<template> <div> <Child /> </div> </template> <script> import Child from "./components/Child"; export default { name: "App", components: { Child, }, provide: { name: "www.baidu.com", }, }; </script>
子組件通過 inject 屬性控制哪些屬性需要訪問
<template> <div>{{ name }}</div> </template> <script> export default { name: "Child", inject: ["name"], }; </script>
父組件獲取子組件數(shù)據(jù)?
通過ref
來獲取子組件的實例,可以獲取子組件的狀態(tài)或者調(diào)用子組件的方法,例如下面
<template> <div> <Child ref="child" /> </div> </template> <script> import Child from "./components/Child"; export default { name: "App", components: { Child, }, mounted() { console.log(this.$refs.child.title); }, }; </script>
可以通過 this.$refs.child 獲取到 Child 組件的實例,訪問 Child 組件的 data
無需考慮組件關(guān)系的通信?
通過 vue 提供的發(fā)布訂閱模式,也叫做 EventBus(事件總線)
定義一個 eventBus 實例
import Vue from "vue"; export default new Vue();
父組件在 mounted 生命周期里面通過 $on 監(jiān)聽 update 事件
<template> <div> <Child : /> </div> </template> <script> import Child from "./components/Child"; import eBus from "../eventBus"; export default { name: "App", data() { return { name: "baidu.com", }; }, components: { Child, }, mounted() { eBus.$on("update", (val) => { this.name = val; }); }, }; </script>
子組件通過 vue 實例的 $emit 觸發(fā) update 事件
<template> <div> {{ name }} <button @click="clickHandle">通知父組件更新</button> </div> </template> <script> import eBus from "../../eventBus"; export default { name: "Child", props: { name: String, }, data() { return { title: "child component", }; }, methods: { clickHandle() { eBus.$emit("update", "Hello World"); }, }, }; </script>
使用全局狀態(tài)管理庫 vuex
具體就不在這里展開講,有興趣的可以查閱 vue 官方文檔
到此這篇關(guān)于Vue組件之間四種通信方式詳解的文章就介紹到這了,更多相關(guān)Vue組件通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue+echarts+datav大屏數(shù)據(jù)展示及實現(xiàn)中國地圖省市縣下鉆功能
這篇文章主要介紹了vue+echarts+datav大屏數(shù)據(jù)展示及實現(xiàn)中國地圖省市縣下鉆,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11Element-UI中關(guān)于table表格的那些騷操作(小結(jié))
這篇文章主要介紹了Element-UI中關(guān)于table表格的那些騷操作(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08vue項目中的public、static及指定不編譯文件問題
這篇文章主要介紹了vue項目中的public、static及指定不編譯文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03vue實現(xiàn)的上拉加載更多數(shù)據(jù)/分頁功能示例
這篇文章主要介紹了vue實現(xiàn)的上拉加載更多數(shù)據(jù)/分頁功能,涉及基于vue的事件響應(yīng)、數(shù)據(jù)交互等相關(guān)操作技巧,需要的朋友可以參考下2019-05-05vue3應(yīng)用elementPlus table并滾動顯示問題
這篇文章主要介紹了vue3應(yīng)用elementPlus table并滾動顯示問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12