小編給大家分享一下vue組件之間相互傳遞數(shù)據(jù)如何實(shí)現(xiàn),希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
站在用戶(hù)的角度思考問(wèn)題,與客戶(hù)深入溝通,找到林芝網(wǎng)站設(shè)計(jì)與林芝網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶(hù)體驗(yàn)好的作品,建站類(lèi)型包括:成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)頁(yè)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋林芝地區(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>
通過(guò)v-bind動(dòng)態(tài)綁定父組件中要傳遞的數(shù)據(jù),子組件通過(guò)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>點(diǎn)擊我向父組件傳值</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>
自定義一個(gè)事件,在子組件中通過(guò)this.$emit()觸發(fā)自定義事件并給父組件傳遞數(shù)據(jù),在父組件中監(jiān)聽(tīng)自定義事件并接收數(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>
看完了這篇文章,相信你對(duì)vue組件之間相互傳遞數(shù)據(jù)如何實(shí)現(xiàn)有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
分享標(biāo)題:vue組件之間相互傳遞數(shù)據(jù)如何實(shí)現(xiàn)
當(dāng)前路徑:http://bm7419.com/article46/gosihg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、定制網(wǎng)站、微信小程序、Google、動(dòng)態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)