vue組件之間的通信

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動(dòng)態(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>點(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è)事件,在子組件中通過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>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在Vue中組件實(shí)例之間的作用域是孤立的,以為不能直接在子組件上引用父組件的數(shù)據(jù),同時(shí)父組件也不能直接使用子組件的數(shù)...
    秋天下雨淋濕冬天閱讀 3,594評(píng)論 0 2
  • 前言 我們平時(shí)做一個(gè)工程項(xiàng)目,每個(gè)功能模塊之間是相互獨(dú)立的,彼此之間按理歷史自理門戶的,但是難眠會(huì)相互通信,而且還...
    i5yue閱讀 300評(píng)論 1 0
  • vue之間通信也挺簡(jiǎn)單的,用到的場(chǎng)景挺多的,簡(jiǎn)單說下。 1 父組件向子組件傳遞數(shù)據(jù) 子組件在父組件的并作為標(biāo)簽引入...
    小強(qiáng)不是蟑螂啊閱讀 2,389評(píng)論 3 80
  • 組件通訊 父?jìng)髯?通過在標(biāo)簽綁定數(shù)據(jù) props:['mag'] 子傳遞父 //子:定義一個(gè)自定義事件 t...
    人言可畏_0292閱讀 218評(píng)論 0 0
  • 今天參加PCB線路板廠家培訓(xùn)楊總監(jiān)用半個(gè)小時(shí)時(shí)間制定了后續(xù)參加培訓(xùn)學(xué)習(xí)的制度表。明確了后續(xù)對(duì)于參加培訓(xùn)遲到、缺席、...
    匯合電路活動(dòng)咨詢閱讀 1,406評(píng)論 0 0

友情鏈接更多精彩內(nèi)容