vue組件之間通信實例總結(jié)(點贊功能)
本文實例講述了vue組件之間通信。分享給大家供大家參考,具體如下:
總結(jié):
父組件--》子組件
①通過屬性
步驟1:
<son myName="michael" myPhone='123'></son> <son :myName="userList[0]"></son>
步驟2:
Vue.component('son',{
props:['myName','myPhone']
})
②通過$parent
直接在子組件中通過this.$parent得到調(diào)用子組件的父組件
子組件--》父組件
①events up
步驟1:在父組件中 調(diào)用子組件的時候 綁定一個自定義事件 和 對應(yīng)的處理函數(shù)
methods:{
recvMsg:function(msg){
//msg就是傳遞來的數(shù)據(jù)
}
},
template:'
<son @customEvent="recvMsg"></son>
'
步驟2:在子組件中 把要發(fā)送的數(shù)據(jù)通過觸發(fā)自定義事件傳遞給父組件
this.$emit('customEvent',123)
②$refs
reference 引用
步驟1:在調(diào)用子組件的時候 可以指定ref屬性
<son ref='zhangsan'></son>
步驟2:通過$refs得到指定引用名稱對應(yīng)的組件實例
this.$refs.zhangsan
兄弟組件通信
步驟1:創(chuàng)建一個Vue的實例 作為事件綁定觸發(fā)的公共的對象
var bus = new Vue();
步驟2:在接收方的組件 綁定 自定義的事件
bus.$on('customEvent',function(msg){
//msg是通過事件傳遞來的數(shù)據(jù) (傳遞來的123)
});
步驟3:在發(fā)送方的組件 觸發(fā) 自定義的事件
bus.$emit('customEvent',123);
每日一練:
創(chuàng)建2個組件,main-component,son-component
視圖:
main-component 顯示一個按鈕
son-component 顯示一個p標(biāo)簽
功能:
main-component 定義一個變量count,初始化為0,將count傳遞給son-component,son-component接收到數(shù)據(jù)顯示在p標(biāo)簽中
main-component 在點擊按鈕時,實現(xiàn)對count的自增操作,要求son-component能夠?qū)崟r顯示count對應(yīng)的數(shù)據(jù)
son-component在接收到count之后,在count大于10的時候,將main-component的按鈕禁用掉
(參考:<button v-bind:disabled="!isValid">clickMe</button>)
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>www.fzitv.net 小練習(xí)</title>
<script src="https://cdn.bootcss.com/vue/2.0.1/vue.min.js"></script>
</head>
<body>
<div id="container">
<p>{{msg}}</p>
<main-component></main-component>
</div>
<script>
/*
每日一練:
創(chuàng)建2個組件,main-component,son-component
視圖:
main-component 顯示一個按鈕
son-component 顯示一個p標(biāo)簽
功能:
main-component 定義一個變量count,初始化為0,將count傳遞給son-component,son-component接收到數(shù)據(jù)顯示在p標(biāo)簽中
main-component 在點擊按鈕時,實現(xiàn)對count的自增操作,要求son-component能夠?qū)崟r顯示count對應(yīng)的數(shù)據(jù)
son-component在接收到count之后,在count大于10的時候,將main-component的按鈕禁用掉
(參考:<button v-bind:disabled="!isValid">clickMe</button>)
*/
//創(chuàng)建父組件
Vue.component("main-component",{
data:function(){
return {
count:0,
isDisabled:true
}
},
methods:{
//點擊按鈕對count進行自增
//并通過$emit觸發(fā)countAdd,并把count的值傳遞給子組件
//判斷count==10的時候讓按鈕禁用
countAdd:function(){
this.count++;
console.log("對數(shù)據(jù)進行自增:"+this.count);
this.$emit("countAdd",this.count);
}
},
template:`
<div>
<button @click="countAdd" v-bind:disabled="!isDisabled">點我</button>
<son-component v-bind:myCount="count"></son-component>
</div>
`
})
//創(chuàng)建子組件
Vue.component("son-component",{
//通過props接收父組件傳遞過來的值
props:["myCount"],
template:`
<div>
<p>{{myCount}}</p>
</div>
`,
//數(shù)據(jù)更新完成后判斷從父組件拿到的值
updated:function(){
if(this.myCount>10){
//子組件通過$parent直接獲取父組件的數(shù)據(jù)
this.$parent.isDisabled = false;
}
}
})
new Vue({
el:"#container",
data:{
msg:"Hello VueJs"
}
})
</script>
</body>
</html>
使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試,可得到如下運行效果:

感興趣的朋友還可以使用上述在線工具測試一下代碼的運行效果。
希望本文所述對大家vue.js程序設(shè)計有所幫助。
相關(guān)文章
基于Vue實現(xiàn)人民幣小寫轉(zhuǎn)為大寫功能
在金融類應(yīng)用中,經(jīng)常需要將金額從小寫數(shù)字轉(zhuǎn)換為大寫形式,這種轉(zhuǎn)換主要用于正式票據(jù)、合同等場合,以增加文本的專業(yè)性和可讀性,本文將詳細(xì)介紹如何在Vue.js項目中實現(xiàn)這一功能,并提供多個示例和詳細(xì)的代碼說明,需要的朋友可以參考下2024-09-09
Vue項目中安裝依賴npm?install一直報錯的解決過程
這篇文章主要給大家介紹了關(guān)于Vue項目中安裝依賴npm?install一直報錯的解決過程,要解決NPM安裝依賴報錯,首先要分析出錯誤的原因,文中將解決的過程介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
Vuex持久化插件(vuex-persistedstate)解決刷新數(shù)據(jù)消失的問題
這篇文章主要介紹了Vuex持久化插件(vuex-persistedstate)-解決刷新數(shù)據(jù)消失的問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
vue v-for出來的列表,點擊某個li使得當(dāng)前被點擊的li字體變紅操作
這篇文章主要介紹了vue v-for出來的列表,點擊某個li使得當(dāng)前被點擊的li字體變紅操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07

