vue---子父、父子、非父子組件通信

子組件和父組件通信

  • 在父組件中使用子組件時(shí)自定義事件,設(shè)置該事件的回調(diào)函數(shù)
  • 在子組件中需要傳數(shù)據(jù)給父組件時(shí)調(diào)用this.$emit觸發(fā)上面自定義的事件,并且設(shè)置要發(fā)生給父組件的數(shù)據(jù)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <p>展示子組件的數(shù)據(jù):{{pMsg}}</p>
        <hr/>
        <my-component v-on:my-click="show"></my-component>
    </div>
    <script src="./vue.js"></script>
    <script>
        //父組件--->使用子組件時(shí)通過(guò)自定義屬性攜帶值--->子組件通過(guò)props接收自定義屬性的值
        //子組件和父組件通信:
        //  在父組件中使用子組件時(shí)自定義事件,設(shè)置該事件的回調(diào)函數(shù)
        //  在子組件中需要傳數(shù)據(jù)給父組件時(shí)調(diào)用this.$emit觸發(fā)上面自定義的事件,并且設(shè)置要發(fā)生給父組件的數(shù)據(jù)

        //組件定義
        const MyComponent={//組件選項(xiàng)
            template:`<div>
                    <input v-model="msg"/>
                    <button @click="send">發(fā)送數(shù)據(jù)給父組件</button>
                </div>`,
            data(){
                return {
                    msg:'hello'
                }
            },
            methods:{
                send(){
                    //發(fā)送是數(shù)據(jù)給父子組件
                    //參數(shù)1指定的事件
                    //參數(shù)2...:發(fā)送給對(duì)應(yīng)事件回調(diào)函數(shù)的數(shù)據(jù)
                    this.$emit('my-click',this.msg);//觸發(fā)某個(gè)事件執(zhí)行

                }
            }
        }

        new Vue({
            el:"#app",
            components:{
                MyComponent     //等效于:MyComponent:MyComponent
            },
            data:{
                pMsg:''
            },
            methods:{
                show(param1){
                    console.log('接收到了子組件的數(shù)據(jù)...',param1);
                    this.pMsg=param1;//將子組件的數(shù)據(jù)賦值給父組件
                }
            }
        })
    </script>
</body>
</html>

父子組件通信

父組件可以使用props屬性將值傳給子組件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <my-component></my-omponent>
    </div>
    <script src="./vue.js"></script>
    <script>
        //定義組件
        const myComponent={
            template:`<div>
                    <h1>{{title}}</h1>
                    <ul>
                        <li v-for="d in list">{{d.name}}</li>
                    </ul>
                </div>`,
           
            //props:['title','list']
            //設(shè)置props驗(yàn)證
            props:{
                //key(所接收到的數(shù)據(jù))
                title:{
                    type:String,
                    // required:true
                    default: 'hello'
                }
            }
        }

        new Vue({
            el:"#app",
            components:{
                myComponent
            }
        })
    
    </script>
</body>
</html>

非父子組件通信

有兩種方法:1)使用第三方來(lái)處理,new vue();2)使用vuex 狀態(tài)管理模式

下面主要講解使用事件總線來(lái)管理

  • 發(fā)送方:
    • 通過(guò)bus.$emit觸發(fā)事件,并且將數(shù)據(jù)作為參數(shù)發(fā)給接收放
    • bus.$emit('my-send',val);
  • 接收方:
    • 通過(guò)bus.$on監(jiān)聽(tīng)事件,監(jiān)聽(tīng)時(shí)設(shè)置回調(diào)函數(shù),當(dāng)事件觸發(fā)時(shí)該回調(diào)函數(shù)就要調(diào)用
    • bus.$on('my-send',function(val){ console.log('收到了發(fā)送方的數(shù)據(jù)',val) })
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <my-component1></my-component1>
        <hr/>
        <my-component2></my-component2>
    </div>
    <script src="./vue.js"></script>
    <script>
        /**
         * 非父子:1)使用new Vue()作為事件總線實(shí)現(xiàn)各個(gè)組件間的通信
         *        2)使用Vuex 狀態(tài)管理模式實(shí)現(xiàn)非父子間的通信
         * 
         * 假設(shè):MyComponent1發(fā)送'hello,組件2'給MyComponent2
         */
        const bus=new Vue();//創(chuàng)建空的Vue實(shí)例充當(dāng)事件總線
        /**
         * 發(fā)送方:
         *     通過(guò)bus.$emit觸發(fā)事件,并且將數(shù)據(jù)作為參數(shù)發(fā)給接收放
         *      bus.$emit('my-send',val);
         * 接收方:
         *     通過(guò)bus.$on監(jiān)聽(tīng)事件,監(jiān)聽(tīng)時(shí)設(shè)置回調(diào)函數(shù),當(dāng)事件觸發(fā)時(shí)該回調(diào)函數(shù)就要調(diào)用
         *      bus.$on('my-send',function(val){
         *              console.log('收到了發(fā)送方的數(shù)據(jù)',val)
         *          })
         */

        const MyComponent1={
            template:`<div>
                            <h1>組件1</h1>
                            <button @click="send">發(fā)送數(shù)據(jù)給組件2</button>
                        </div>`,
            data(){
                return {
                    msg:'hello,組件2'
                }
            },
            methods:{
                //發(fā)送數(shù)據(jù)
                send(){
                    bus.$emit('my-send',this.msg);
                }
            }
        }

        const MyComponent2={
            template:`
                <div>
                    <h2>組件2..</h2>
                    <p>{{msg}}</p>
                </div>
            `,
            data(){
                return {
                    msg:''
                }
            },
            created() {
                //接收數(shù)據(jù)
                bus.$on('my-send',(val)=>{
                    this.msg=val
                })
            },  
        }
        new Vue({
            el:"#app",
            components:{
                MyComponent1,
                MyComponent2
            }
        })
    </script>
</body>
</html>
?著作權(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也差不多半年有余了,有些東西還感覺(jué)有點(diǎn)模糊,現(xiàn)在又學(xué)一遍好好總結(jié)一下,夯實(shí)基礎(chǔ)直接進(jìn)入主題 1,父子組件...
    90c9522b1be3閱讀 873評(píng)論 0 4
  • 前言 您將在本文當(dāng)中了解到,往網(wǎng)頁(yè)中添加數(shù)據(jù),從傳統(tǒng)的dom操作過(guò)渡到數(shù)據(jù)層操作,實(shí)現(xiàn)同一個(gè)目標(biāo),兩種不同的方式....
    itclanCoder閱讀 26,245評(píng)論 1 12
  • 組件(Component)是Vue.js最核心的功能,也是整個(gè)架構(gòu)設(shè)計(jì)最精彩的地方,當(dāng)然也是最難掌握的。...
    六個(gè)周閱讀 5,775評(píng)論 0 32
  • 對(duì)于vue來(lái)說(shuō),組件之間的消息傳遞是非常重要的,下面是我對(duì)組件之間消息傳遞的各種方式的總結(jié),總共有8種方式。 1....
    edc余悸閱讀 427評(píng)論 0 3
  • 早晨,我蜷縮坐在碼頭上。雙手抱膝,看著被海水常年拍擊腐蝕的木樁。大海很藍(lán)很藍(lán),海面上有幾只海鷗在不停地盤旋嬉鬧。...
    人間慢步閱讀 1,002評(píng)論 0 2

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