DIV CSS 佈局教程網

Vue.js 父子組件通信完全指南(附完整範例)
編輯:關於JavaScript     

本篇整理了官方文件中的重點,並補充了更細緻、程式碼更完整的說明,非常適合新手閱讀。

資料來源為官方文件:
https://cn.vuejs.org/guide/components.html#u7236_u5B50_u7EC4_u4EF6_u901A_u4FE1

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

this.$parent 用來訪問父組件;this.$children 用來訪問子組件(是一個陣列);this.$root 則讓根實例的後代能訪問根實例。

 <div id="app"> 父組件: <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 id="app"> 父組件: <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>

說明:

【1】點擊父組件的按鈕,會向下廣播,然後觸發子組件1本身、另一個子組件1,以及另一個子組件2;

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

【3】點擊另一個子組件2的按鈕,會觸發另一個子組件2、另一個子組件1與父組件的事件(因為另一個子組件1的事件返回值為 true)。

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

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

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

【3】子組件派發消息傳遞的參數,即使子組件的事件沒有參數,也不影響將參數傳遞給父組件的方法。

 <div id="app"> 父組件: <button>點擊向下傳播 broadcast</button> <br/> 子組件1: <!--綁定寫在這裡,可以多個綁定同一個,或不同綁定不同的,但不能一個綁定多個--> <children v-on:test="parent" @test2="another"></children> </div> <script> var vm = new Vue({ el: '#app', data: { val: 1 }, methods: { parent: function (arg) { console.log(arg); console.log("the first method with test event"); }, another: function () { console.log("another method"); } }, components: { children: { //這個無返回值,不會繼續派發 props: ['test'], template: "<button @click='childClick'>children1</button></br><button @click='childClick2'>children1</button>", methods: { childClick: function () { this.$emit("test", 'the argument for dispatch'); }, childClick2: function () { this.$emit("test2"); } }, events: { test: function () { console.log("test"); }, test2: function () { console.log("test2"); } } } } }); </script>

④ 子組件索引

簡單來說,就是可以直接從索引取得子組件,然後調用各個子組件的方法。

添加索引的方法是:在標籤裡添加 v-ref:索引名;

調用組件的方法是:vm.$refs.索引名,也可以直接在父組件中使用 this.$refs.索引名。

如此一來就能取得組件,進而調用它的方法或使用其數據。

 <div id="app"> 父組件: <button @click="todo">觸發子組件的事件</button> <br/> 子組件1: <children v-ref:child></children> </div> <script> var vm = new Vue({ el: '#app', methods: { todo: function () { this.$refs.child.fromParent(); //透過索引調用子組件的 fromParent 方法 } }, components: { children: { //這個無返回值,不會繼續派發 props: ['test'], template: "<button>children1</button>", methods: { fromParent: function () { console.log("happened fromParent by ref"); } } } } }); </script>

以上便是 Vue.js 父子組件通信的完整整理,希望對大家有所幫助。若有任何疑問歡迎留言,我會及時回覆。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved