溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

vue組件之間相互傳遞數(shù)據(jù)如何實現(xiàn)

發(fā)布時間:2020-10-22 14:43:38 來源:億速云 閱讀:172 作者:小新 欄目:web開發(fā)

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

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)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI