對於vue來說,組件之間的消息傳遞是非常重要的,下面是我對組件之間消息傳遞的常用方式的總結。
- props和$emit(常用)
- $attrs和$listeners
- 中央事件總線(非父子組件間通信)
- v-model
- provide和inject
- $parent和$children
- vuex
1.props和$emit
父組件向子組件傳遞數據是通過prop傳遞的,子組件傳遞數據給父組件是通過$emit觸發事件來做到的.
1 Vue.component('child',{ 2 data(){ 3 return { 4 mymessage:this.message 5 } 6 }, 7 template:` 8 <div> 9 <input type="text" v-model="mymessage" @input="passData(mymessage)"> </div> 10 `, 11 props:['message'],//設置props屬性值,得到父組件傳遞過來的數據 12 methods:{ 13 passData(val){ 14 //觸發父組件中的事件,向父組件傳值 15 this.$emit('getChildData',val) 16 } 17 } 18 }) 19 Vue.component('parent',{ 20 template:` 21 <div> 22 <p>this is parent compoent!</p> 23 <child :message="message" v-on:getChildData="getChildData"></child> 24 </div> 25 `, 26 data(){ 27 return { 28 message:'hello' 29 } 30 }, 31 methods:{ 32 //執行子組件觸發的事件 33 getChildData(val){ 34 console.log(val) 35 } 36 } 37 })
在上面的例子中,有父組件parent和子組件child。
1).父組件傳遞了message數據給子組件,並且通過v-on綁定了一個getChildData事件來監聽子組件的觸發事件;
2).子組件通過props得到相關的message數據,最后通過this.$emit觸發了getChildData事件
2.$attrs和$listeners
第一種方式處理父子組件之間的數據傳輸有一個問題:如果父組件A下面有子組件B,組件B下面有組件C,這時如果組件A想傳遞數據給組件C怎么辦呢? 如果采用第一種方法,我們必須讓組件A通過prop傳遞消息給組件B,組件B在通過prop傳遞消息給組件C;要是組件A和組件C之間有更多的組件,那采用這種方式就很復雜了。Vue 2.4開始提供了$attrs和$listeners來解決這個問題,能夠讓組件A之間傳遞消息給組件C。
1 Vue.component('C',{ 2 template:` 3 <div> 4 <input type="text" v-model="$attrs.messagec" @input="passCData($attrs.messagec)"> </div> 5 `, 6 methods:{ 7 passCData(val){ 8 //觸發父組件A中的事件 9 this.$emit('getCData',val) 10 } 11 } 12 }) 13 Vue.component('B',{ 14 data(){ 15 return { 16 mymessage:this.message 17 } 18 }, 19 template:` 20 <div> 21 <input type="text" v-model="mymessage" @input="passData(mymessage)"> 22 <!-- C組件中能直接觸發getCData的原因在於 B組件調用C組件時 使用 v-on 綁定了$listeners 屬性 --> 23 <!-- 通過v-bind 綁定$attrs屬性,C組件可以直接獲取到A組件中傳遞下來的props(除了B組件中props聲明的) --> 24 <C v-bind="$attrs" v-on="$listeners"></C> 25 </div> 26 `, 27 props:['message'],//得到父組件傳遞過來的數據 28 methods:{ 29 passData(val){ 30 //觸發父組件中的事件 31 this.$emit('getChildData',val) 32 } 33 } 34 }) 35 Vue.component('A',{ 36 template:` 37 <div> 38 <p>this is parent compoent!</p> 39 <B :messagec="messagec" :message="message" v-on:getCData="getCData" v-on:getChildData="getChildData(message)"></B> 40 </div> 41 `, 42 data(){ 43 return { 44 message:'hello', 45 messagec:'hello c' //傳遞給c組件的數據 46 } 47 }, 48 methods:{ 49 getChildData(val){ 50 console.log('這是來自B組件的數據') 51 }, 52 //執行C子組件觸發的事件 53 getCData(val){ 54 console.log("這是來自C組件的數據:"+val) 55 } 56 } 57 })
3. 中央事件總線
上面兩種方式處理的都是父子組件之間的數據傳遞,而如果兩個組件不是父子關系呢?這種情況下可以使用中央事件總線的方式。新建一個Vue事件bus對象,然后通過bus.$emit觸發事件,bus.$on監聽觸發的事件。
Vue.component('brother1',{ data(){ return { mymessage:'hello brother1' } }, template:` <div> <p>this is brother1 compoent!</p> <input type="text" v-model="mymessage" @input="passData(mymessage)"> </div> `, methods:{ passData(val){ //觸發全局事件globalEvent bus.$emit('globalEvent',val) } } }) Vue.component('brother2',{ template:` <div> <p>this is brother2 compoent!</p> <p>brother1傳遞過來的數據:{{brothermessage}}</p> </div> `, data(){ return { mymessage:'hello brother2', brothermessage:'' } }, mounted(){ //綁定全局事件globalEvent bus.$on('globalEvent',(val)=>{ this.brothermessage=val; }) } }) //中央事件總線 var bus=new Vue(); var app=new Vue({ el:'#app', template:` <div> <brother1></brother1> <brother2></brother2> </div> ` })
4. provide和inject
在 Vue.js 的 2.2.0+
版本中添加加了 provide 和 inject 選項。他們成對出現,用於父級組件向下傳遞數據。
父組件中通過provider來提供變量,然后在子組件中通過inject來注入變量。不論子組件有多深,只要調用了inject那么就可以注入provider中的數據。而不是局限於只能從當前父組件的prop屬性來獲取數據,只要在父組件的生命周期內,子組件都可以調用。
1 Vue.component('child',{ 2 inject:['for'],//得到父組件傳遞過來的數據 3 data(){ 4 return { 5 mymessage:this.for 6 } 7 }, 8 template:` 9 <div> 10 <input type="tet" v-model="mymessage"> 11 </div> 12 }) 13 Vue.component('parent',{ 14 template:` 15 <div> 16 <p>this is parent compoent!</p> 17 <child></child> 18 </div> 19 `, 20 provide:{ 21 for:'test' 22 }, 23 data(){ 24 return { 25 message:'hello' 26 } 27 } 28 })
5. v-model
父組件通過v-model傳遞值給子組件時,會自動傳遞一個value的prop屬性,在子組件中通過this.$emit(‘input',val)自動修改v-model綁定的值
Vue.component('child',{ props:{ value:String, //v-model會自動傳遞一個字段為value的prop屬性 }, data(){ return { mymessage:this.value } }, methods:{ changeValue(){ this.$emit('input',this.mymessage);//通過如此調用可以改變父組件上v-model綁定的值 } }, template:` <div> <input type="text" v-model="mymessage" @change="changeValue"> </div> }) Vue.component('parent',{ template:` <div> <p>this is parent compoent!</p> <p>{{message}}</p> <child v-model="message"></child> </div> `, data(){ return { message:'hello' } } }) var app=new Vue({ el:'#app', template:` <div> <parent></parent> </div> ` })
6. $parent和$children
在組件內部可以直接通過子組件$parent對父組件進行操作,父組件通過$children對子組件進行操作.
Vue.component('child',{ props:{ value:String, //v-model會自動傳遞一個字段為value的prop屬性 }, data(){ return { mymessage:this.value } }, methods:{ changeValue(){ this.$parent.message = this.mymessage;//通過如此調用可以改變父組件的值 } }, template:` <div> <input type="text" v-model="mymessage" @change="changeValue"> </div> }) Vue.component('parent',{ template:` <div> <p>this is parent compoent!</p> <button @click="changeChildValue">test</button > <child></child> </div> `, methods:{ changeChildValue(){ this.$children[0].mymessage = 'hello'; } }, data(){ return { message:'hello' } } }) var app=new Vue({ el:'#app', template:` <div> <parent></parent> </div> ` })
7. vuex處理組件之間的數據交互
如果業務邏輯復雜,很多組件之間需要同時處理一些公共的數據,這個時候才有上面這一些方法可能不利於項目的維護,vuex的做法就是將這一些公共的數據抽離出來,然后其他組件就可以對這個公共數據進行讀寫操作,這樣達到了解耦的目的。
參考鏈接:https://my.oschina.net/u/3982182/blog/3019264