Vuejs——(10)組件——父子組件通訊

 

目錄(?)[+]vue

 

 

本篇資料來於官方文檔:數組

http://cn.vuejs.org/guide/components.html#u7236_u5B50_u7EC4_u4EF6_u901A_u4FE1
app

本文是在官方文檔的基礎上,更加細緻的說明,代碼更多更全。ide

簡單來講,更適合新手閱讀ui

 

(二十七)父子組件通訊this

①訪問子組件、父組件、根組件;

 

this.$parent    訪問父組件spa

this.$children   訪問子組件(是一個數組).net

this.$root            根實例的後代訪問根實例code

示例代碼:

[html]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. <div id="app">  
  2.     父組件:  
  3.     <input v-model="val"><br/>  
  4.     子組件:  
  5.     <test :test="val"></test>  
  6. </div>  
  7. <script>  
  8.     var vm = new Vue({  
  9.         el: '#app',  
  10.         data: {  
  11.             val: 1  
  12.         },  
  13.         components: {  
  14.             test: {  
  15.                 props: ['test'],  
  16.                 template: "<input @keyup='findParent' v-model='test'/>",  
  17.                 methods: {  
  18.                     findParent: function () {  
  19.                         console.log(this.$parent);  //訪問根組件  
  20.                         console.log(this.$parent.val);  //訪問根組件的val屬性  
  21.                         console.log(this.$parent.$children.indexOf(this));  //查看當前可否在其父組件的子組件中找到索引  
  22.                         console.log(this.$parent === this.$root);   //查看父組件和根組件是否是全等的(由於他的父組件就是根組件)  
  23.                     }  
  24.                 }  
  25.             }  
  26.         }  
  27.     });  
  28. </script>  

當在子組件的輸入框按鍵彈起時,顯示內容依次爲:

父組件、父組件的輸入框的值(默認狀況是1)、0(表示是父組件的children屬性中的第一個元素)、true(因爲父組件就是根組件,因此是全等的);

 

經過這樣的方法,能夠在組件樹中進行互動。

 

②自定義事件:

首先,事件須要放置在events屬性之中,而不是放置在methods屬性中(新手很容易犯的錯誤),只能觸發events屬性中的事件,而methods屬性中的事件是沒法觸發的。

事件

說明

$on(事件名)

事件名的類型是字符串(下同),調用它能夠經過this.$on()來調用;

$emit(事件名, 參數)

用於觸發事件,參數是用於傳遞給事件的參數。這個用於觸發同級事件(當前組件的)

$dispatch(事件名, 參數)

①向上派發事件,用於向父組件傳播。

②會首先觸發當前組件的同名事件(若是有);

③而後會向上冒泡,當遇到第一個符合的父組件的事件後觸發並中止;

④當父組件的事件的返回值設爲true會繼續冒泡去找下一個。

$broadcast(事件名, 參數)

①向下廣播事件,用於向全部子組件傳播。

②默認狀況是僅限子組件;

③子組件事件的返回值是true,纔會繼續向該子組件的孫組件派發;

④不會觸發自身同名事件;

 

其次,向上派發和向下廣播有所區別:向上派發會觸發自身同名事件,而向下廣播不會;

 

第三,向上派發和向下廣播默認只會觸發直系(子或者父,不包括祖先和孫)的事件,除非事件返回值爲true,纔會繼續在這一條線上繼續。

 

第四,事件不能顯式的經過 this.事件名 來調用它。

 

示例代碼:

[html]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. <div id="app">  
  2.     父組件:  
  3.     <button @click="parentClick">點擊向下傳播broadcast</button>  
  4.     <br/>  
  5.     子組件1:  
  6.     <children1></children1>  
  7.     <br/>  
  8.     另外一個子組件1:  
  9.     <another-children1></another-children1>  
  10. </div>  
  11. <script>  
  12.     var vm = new Vue({  
  13.         el: '#app',  
  14.         data: {  
  15.             val: 1  
  16.         },  
  17.         methods: {  
  18.             parentClick: function () {  
  19.                 this.$broadcast("parentClick", "abc");  
  20.             }  
  21.         },  
  22.         events: {  
  23.             childrenClick: function () {  
  24.                 console.log("childrenClick-Parent");  
  25.             },  
  26.             parentClick: function () {  
  27.                 console.log("parentClick-Parent");  
  28.                 return true;  
  29.             }  
  30.         },  
  31.         components: {  
  32.             children1: {    //這個無返回值,不會繼續派發  
  33.                 props: ['test'],  
  34.                 template: "<button>children1</button></br>子組件2:<children2></children2>",  
  35.                 events: {  
  36.                     childrenClick: function () {  
  37.                         console.log("childrenClick-children1");  
  38.                     },  
  39.                     parentClick: function (msg) {  
  40.                         console.log("parentClick-Children1");  
  41.                         console.log("message:" + msg);  
  42.                     }  
  43.                 },  
  44.                 components: {  
  45.                     children2: {  
  46.                         props: ['test'],  
  47.                         template: "<button @click='findParent'>children-Click</button>",  
  48.                         methods: {  
  49.                             findParent: function () {  
  50.                                 this.$dispatch('childrenClick');  
  51.                             }  
  52.                         },  
  53.                         events: {  
  54.                             childrenClick: function () {  
  55.                                 console.log("childrenClick-children2");  
  56.                             },  
  57.                             parentClick: function (msg) {  
  58.                                 console.log("parentClick-Children2");  
  59.                                 console.log("message:" + msg);  
  60.                             }  
  61.                         }  
  62.                     }  
  63.                 }  
  64.             },  
  65.             anotherChildren1: { //這個是返回值爲true,會繼續向子組件的子組件派發  
  66.                 props: ['test'],  
  67.                 template: "<button>anotherChildren1</button></br>另外一個子組件2:<another-children2></another-children2>",  
  68.                 events: {  
  69.                     childrenClick: function () {  
  70.                         console.log("childrenClick-anotherChildren1");  
  71.                         return true;  
  72.                     },  
  73.                     parentClick: function (msg) {  
  74.                         console.log("parentClick-anotherChildren1");  
  75.                         console.log("message:" + msg);  
  76.                         return true;  
  77.                     }  
  78.                 },  
  79.                 components: {  
  80.                     anotherChildren2: {  
  81.                         props: ['test'],  
  82.                         template: "<button @click='findParent'>anotherChildren2-Click</button>",  
  83.                         methods: {  
  84.                             findParent: function () {  
  85.                                 this.$dispatch('childrenClick');  
  86.                             }  
  87.                         },  
  88.                         events: {  
  89.                             childrenClick: function () {  
  90.                                 console.log("childrenClick-anotherChildren2");  
  91.                             },  
  92.                             parentClick: function (msg) {  
  93.                                 console.log("parentClick-anotherChildren2");  
  94.                                 console.log("message:" + msg);  
  95.                             }  
  96.                         }  
  97.                     }  
  98.                 }  
  99.             }  
  100.   
  101.         }  
  102.     });  
  103. </script>  
  104.                },  
  105.                             parentClick: function () {  
  106.                                 console.log("parentClick-anotherChildren2");  
  107.                             }  
  108.                         }  
  109.                     }  
  110.                 }  
  111.             }  
  112.   
  113.         }  
  114.     });  
  115. </script>  

 

說明:

【1】點擊父組件的按鈕,會向下廣播,而後觸發子組件1自己,另一個子組件1,以及另外一個子組件2;

【2】點擊子組件2的按鈕,會觸發子組件2的事件和子組件1的事件,但不會觸發父組件的按鈕;

【3】點擊另外一個子組件2的按鈕,會觸發另外一個子組件2的事件,另外一個子組件1的事件和父組件的事件(由於另外一個子組件1的事件的返回值爲true);

 

 

 

③使用v-on綁定自定義事件:

【1】簡單來講,子組件觸發某個事件(events裏的方法)時,父組件也會執行某個方法(父組件methods裏的方法)。

 

【2】觸發的綁定寫在模板之中(即被替換的那個template模板中),能夠多個子組件的事件綁定一個父組件的方法,或者不一樣子組件的事情綁定不一樣父組件的方法,可是不能同一個子組件事件綁定多個父組件的方法。

 

【3】子組件派發消息傳遞的參數,即便子組件的事件沒有參數,也不影響將參數傳遞給父組件的方法(即父組件的方法能夠接受到子組件方法獲取的參數)

 

如示例:

[html]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. <div id="app">  
  2.     父組件:  
  3.     <button>點擊向下傳播broadcast</button>  
  4.     <br/>  
  5.     子組件1:  
  6.     <!--綁定寫在這裏,能夠多個綁定同一個,或者不一樣綁定不一樣的,但不能一個綁定多個-->  
  7.     <children v-on:test="parent" @test2="another"></children>  
  8. </div>  
  9. <script>  
  10.     var vm = new Vue({  
  11.         el: '#app',  
  12.         data: {  
  13.             val: 1  
  14.         },  
  15.         methods: {  
  16.             parent: function (arg) {  
  17.                 console.log(arg);  
  18.                 console.log("the first method with test event");  
  19.             },  
  20.             another: function () {  
  21.                 console.log("another method");  
  22.             }  
  23.         },  
  24.         components: {  
  25.             children: {    //這個無返回值,不會繼續派發  
  26.                 props: ['test'],  
  27.                 template: "<button @click='childClick'>children1</button></br><button @click='childClick2'>children1</button>",  
  28.                 methods: {  
  29.                     childClick: function () {  
  30.                         this.$emit("test", 'the argument for dispatch');  
  31.                     },  
  32.                     childClick2: function () {  
  33.                         this.$emit("test2");  
  34.                     }  
  35.                 },  
  36.                 events: {  
  37.                     test: function () {  
  38.                         console.log("test");  
  39.                     },  
  40.                     test2: function () {  
  41.                         console.log("test2");  
  42.                     }  
  43.                 }  
  44.             }  
  45.         }  
  46.     });  
  47. </script>  

 

④子組件索引

簡單來講:就是能夠直接從索引獲取到子組件,而後就能夠調用各個子組件的方法了。

 

添加索引方法是:在標籤裏添加v-ref:索引名

調用組件方法是:vm.$ref.索引名

也能夠直接在父組件中使用this.$ref.索引名

這個時候,就能夠得到組件了,而後經過組件能夠調用他的方法,或者是使用其數據。

 

示例代碼:

[html]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. <div id="app">  
  2.     父組件:  
  3.     <button @click="todo">觸發子組件的事件</button>  
  4.     <br/>  
  5.     子組件1:  
  6.     <!--綁定寫在這裏,能夠多個綁定同一個,或者不一樣綁定不一樣的,但不能一個綁定多個-->  
  7.     <children v-ref:child></children>  
  8. </div>  
  9. <script>  
  10.     var vm = new Vue({  
  11.         el: '#app',  
  12.         methods: {  
  13.             todo: function () {  
  14.                 this.$refs.child.fromParent();  //經過索引調用子組件的fromParent方法  
  15.             }  
  16.         },  
  17.         components: {  
  18.             children: {    //這個無返回值,不會繼續派發  
  19.                 props: ['test'],  
  20.                 template: "<button>children1</button>",  
  21.                 methods: {  
  22.                     fromParent: function () {  
  23.                         console.log("happened fromParent by ref");  
  24.                     }  
  25.                 }  
  26.             }  
  27.         }  
  28.     });  
  29. </script>  
相關文章
相關標籤/搜索