本篇文章和大家了解一下Vue常用的組件通信方式有哪些。有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)大家有所幫助。
目前創(chuàng)新互聯(lián)建站已為成百上千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、豐林網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
組建通信的基本模式:父子組件的關(guān)系可以總結(jié)為 prop 向下傳遞,事件向上傳遞。父組件通過(guò) prop 給子組件下發(fā)數(shù)據(jù),子組件通過(guò)事件給父組件發(fā)送消息
組件通信的常用三種方式
parent.vue
<template> <p> <parent-child :childName="childName"></parent-child> </p> </template> <script> import child from "./child" export default { components: { "parent-child" : child },data(){ return { childName : "我是child哦" } } } </script>
child.vue
<template> <p id="child"> child的名字叫:{{childName}}<br> </p> </template> <script> export default { props:{ childName :{ type:String, default: "" } } } </script>
parent.vue
<template> <p> <parent-child :childName="childName" @childNotify="childReceive"></parent-child> </p> </template> <script> import child from "./child" export default { components: { "parent-child" : child },data(){ return { childName : "我是child哦" } },methods:{ childReceive(params){ this.$message(params) } } } </script>
child.vue
<template> <p id="child"> child的名字叫:{{childName}}<br> </p> </template> <script> export default { props:{ childName :{ type:String, default: "" } },methods:{ childNotify(params){ this.$emit("childNotify","child的名字叫"+this.childName); } } } </script>
bus.js
const install = (Vue) => { const Bus = new Vue({ methods: { on (event, ...args) { this.$on(event, ...args); }, emit (event, callback) { this.$emit(event, callback); }, off (event, callback) { this.$off(event, callback); } } }) Vue.prototype.$bus = Bus; } export default install;
main.js
import Bus from "./assets/js/bus"; Vue.use(Bus);
child.vue
<template> <p id="child"> child的名字叫:{{childName}}<br> <el-button type="primary" @click="brotherNotity">向child2打招呼</el-button> </p> </template> <script> export default { props:{ childName :{ type:String, default: "" } },methods:{ childNotify(params){ this.$emit("childNotify","child的名字叫"+this.childName); }, brotherNotity(){ this.$bus.$emit("child2","child2你好呀"); } } } </script>
child2.vue
<template> <p id="child2"> child2的名字叫:{{child2Name}} </p> </template> <script> export default { props:{ child2Name :{ type:String, default: "" } }, created(){ this.$bus.$on("child2",function(params){ this.$message(params) }) }, beforeDestroy() { this.$bus.$off("child2",function(){ this.$message("child2-bus銷毀") }) } } </script>
Vue是一套用于構(gòu)建用戶界面的漸進(jìn)式JavaScript框架,Vue與其它大型框架的區(qū)別是,使用Vue可以自底向上逐層應(yīng)用,其核心庫(kù)只關(guān)注視圖層,方便與第三方庫(kù)和項(xiàng)目整合,且使用Vue可以采用單文件組件和Vue生態(tài)系統(tǒng)支持的庫(kù)開(kāi)發(fā)復(fù)雜的單頁(yè)應(yīng)用。
以上就是Vue常用的組件通信方式有哪些的簡(jiǎn)略介紹,當(dāng)然詳細(xì)使用上面的不同還得要大家自己使用過(guò)才領(lǐng)會(huì)。如果想了解更多,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道哦!
分享名稱:Vue常用的組件通信方式有哪些
分享URL:http://vcdvsql.cn/article40/pdspeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站設(shè)計(jì)、用戶體驗(yàn)、、網(wǎng)站維護(hù)、定制開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)