小編給大家分享一下vue組件之間相互傳遞數(shù)據(jù)如何實現(xiàn),希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

站在用戶的角度思考問題,與客戶深入溝通,找到林芝網(wǎng)站設(shè)計與林芝網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站設(shè)計、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務覆蓋林芝地區(qū)。
1、子組件給父組件傳遞數(shù)據(jù)
<body>
<div id="app">
父組件:{{total}}
<br>
<son-component v-bind:total="total"></son-component>
</div>
<script>
Vue.component('son-component',{
template:'<div>子組件:{{total}}+{{num}}={{add}}</div>',
props:{
total:Number
},
data(){
return {
num:10
}
},
computed:{
add:function(){
return num=this.total+this.num
}
}
})
var app=new Vue({
el:'#app',
data:{
total:1
},
})
</script>
</body>通過v-bind動態(tài)綁定父組件中要傳遞的數(shù)據(jù),子組件通過props屬性接收父組件傳遞的數(shù)據(jù)。
2.父組件給子組件傳遞數(shù)據(jù)
<body>
<div id="app">
<son-component v-on:change="getData"></son-component>
<br>
{{total}}
</div>
<script>
Vue.component('son-component',{
template:'<button v-on:click=sendData>點擊我向父組件傳值</button>',
data(){
return{
count:1
}
},
methods:{
sendData:function(){
this.$emit('change',this.count)
}
}
})
var app=new Vue({
el:'#app',
data:{
total:1
},
methods:{
getData:function(value){
this.total=this.total+value
}
}
})
</script>
</body>自定義一個事件,在子組件中通過this.$emit()觸發(fā)自定義事件并給父組件傳遞數(shù)據(jù),在父組件中監(jiān)聽自定義事件并接收數(shù)據(jù)。
3.非父子組件之間的通信
<body>
<div id="app">
<a-component></a-component>
<b-component></b-component>
</div>
<script>
Vue.component('a-component',{
template:`
<div>
<span>a組件的數(shù)據(jù):{{num}}</span><br>
<button v-on:click="sendData">擊我向b組件傳遞數(shù)據(jù)</button>
</div>
`,
data(){
return {
num:1
}
},
methods:{
sendData:function(){
this.$root.bus.$emit('change',this.num)
}
}
})
Vue.component('b-component',{
template:`
<div>b組件接收a組件數(shù)據(jù)后相加的數(shù)據(jù):{{count}}</div>
`,
data(){
return {
count: 10
}
},
created:function(){
this.$root.bus.$on('change',(value)=>{
//alert(value)
this.count=this.count+value
})
}
})
var app=new Vue({
el:'#app',
data:{
bus:new Vue()
},
})
</script>
</body>看完了這篇文章,相信你對vue組件之間相互傳遞數(shù)據(jù)如何實現(xiàn)有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
分享標題:vue組件之間相互傳遞數(shù)據(jù)如何實現(xiàn)
當前路徑:http://chinadenli.net/article46/gosihg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、定制網(wǎng)站、微信小程序、Google、動態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)