本篇文章是小編結合官方文檔整理的一套更加細致,代碼更多更全的教程,非常不錯,比較適合新手閱讀。
本篇資料來于官方文檔:
http://cn.vuejs.org/guide/components.html#u7236_u5B50_u7EC4_u4EF6_u901A_u4FE1
父子組件通信
①訪問子組件、父組件、根組件;
this.$parent 訪問父組件
this.$children 訪問子組件(是一個數組)
this.$root 根實例的后代訪問根實例
示例代碼:
<div > 父組件: <input v-model="val"><br/> 子組件: <test :test="val"></test> </div> <script> var vm = new Vue({ el: '#app', data: { val: 1 }, components: { test: { props: ['test'], template: "<input @keyup='findParent' v-model='test'/>", methods: { findParent: function () { console.log(this.$parent); //訪問根組件 console.log(this.$parent.val); //訪問根組件的val屬性 console.log(this.$parent.$children.indexOf(this)); //查看當前能否在其父組件的子組件中找到索引 console.log(this.$parent === this.$root); //查看父組件和根組件是不是全等的(因為他的父組件就是根組件) } } } } }); </script>
當在子組件的輸入框按鍵彈起時,顯示內容依次為:
父組件、父組件的輸入框的值(默認情況是1)、0(表示是父組件的children屬性中的第一個元素)、true(由于父組件就是根組件,所以是全等的);
通過這樣的方法,可以在組件樹中進行互動。
②自定義事件:
首先,事件需要放置在events屬性之中,而不是放置在methods屬性中(新手很容易犯的錯誤),只能觸發events屬性中的事件,而methods屬性中的事件是無法觸發的。
其次,向上派發和向下廣播有所區別:向上派發會觸發自身同名事件,而向下廣播不會;
第三,向上派發和向下廣播默認只會觸發直系(子或者父,不包括祖先和孫)的事件,除非事件返回值為true,才會繼續在這一條線上繼續。
第四,事件不能顯式的通過 this.事件名 來調用它。
示例代碼:
<div > 父組件: <button @click="parentClick">點擊向下傳播broadcast</button> <br/> 子組件1: <children1></children1> <br/> 另一個子組件1: <another-children1></another-children1> </div> <script> var vm = new Vue({ el: '#app', data: { val: 1 }, methods: { parentClick: function () { this.$broadcast("parentClick", "abc"); } }, events: { childrenClick: function () { console.log("childrenClick-Parent"); }, parentClick: function () { console.log("parentClick-Parent"); return true; } }, components: { children1: { //這個無返回值,不會繼續派發 props: ['test'], template: "<button>children1</button></br>子組件2:<children2></children2>", events: { childrenClick: function () { console.log("childrenClick-children1"); }, parentClick: function (msg) { console.log("parentClick-Children1"); console.log("message:" + msg); } }, components: { children2: { props: ['test'], template: "<button @click='findParent'>children-Click</button>", methods: { findParent: function () { this.$dispatch('childrenClick'); } }, events: { childrenClick: function () { console.log("childrenClick-children2"); }, parentClick: function (msg) { console.log("parentClick-Children2"); console.log("message:" + msg); } } } } }, anotherChildren1: { //這個是返回值為true,會繼續向子組件的子組件派發 props: ['test'], template: "<button>anotherChildren1</button></br>另一個子組件2:<another-children2></another-children2>", events: { childrenClick: function () { console.log("childrenClick-anotherChildren1"); return true; }, parentClick: function (msg) { console.log("parentClick-anotherChildren1"); console.log("message:" + msg); return true; } }, components: { anotherChildren2: { props: ['test'], template: "<button @click='findParent'>anotherChildren2-Click</button>", methods: { findParent: function () { this.$dispatch('childrenClick'); } }, events: { childrenClick: function () { console.log("childrenClick-anotherChildren2"); }, parentClick: function (msg) { console.log("parentClick-anotherChildren2"); console.log("message:" + msg); } } } } } } }); </script> }, parentClick: function () { console.log("parentClick-anotherChildren2"); } } } } } } }); </script>
新聞熱點
疑難解答