国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

vue組件之間通信方式實例總結【8種方式】

2019-11-19 12:04:54
字體:
來源:轉載
供稿:網友

本文實例總結了vue組件之間通信方式。分享給大家供大家參考,具體如下:

對于vue來說,組件之間的消息傳遞是非常重要的,下面是我對組件之間消息傳遞的各種方式的總結,總共有8種方式。

1. props和$emit

父組件向子組件傳遞數據是通過prop傳遞的,子組件傳遞數據給父組件是通過$emit觸發事件來做到的。

Vue.component('child',{    data(){      return {        mymessage:this.message      }    },    template:`      <div>        <input type="text" v-model="mymessage" @input="passData(mymessage)"> </div>    `,    props:['message'],//得到父組件傳遞過來的數據    methods:{      passData(val){        //觸發父組件中的事件        this.$emit('getChildData',val)      }    }  })  Vue.component('parent',{    template:`      <div>        <p>this is parent compoent!</p>        <child :message="message" v-on:getChildData="getChildData"></child>      </div>    `,    data(){      return {        message:'hello'      }    },    methods:{      //執行子組件觸發的事件      getChildData(val){        console.log(val)      }    }  })  var app=new Vue({    el:'#app',    template:`      <div>        <parent></parent>      </div>    `  })

在上面的例子中,有父組件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。

Vue.component('C',{    template:`      <div>        <input type="text" v-model="$attrs.messagec" @input="passCData($attrs.messagec)"> </div>    `,    methods:{      passCData(val){        //觸發父組件A中的事件        this.$emit('getCData',val)      }    }  })  Vue.component('B',{    data(){      return {        mymessage:this.message      }    },    template:`      <div>        <input type="text" v-model="mymessage" @input="passData(mymessage)">        <!-- C組件中能直接觸發getCData的原因在于 B組件調用C組件時 使用 v-on 綁定了$listeners 屬性 -->        <!-- 通過v-bind 綁定$attrs屬性,C組件可以直接獲取到A組件中傳遞下來的props(除了B組件中props聲明的) -->        <C v-bind="$attrs" v-on="$listeners"></C>      </div>    `,    props:['message'],//得到父組件傳遞過來的數據    methods:{      passData(val){        //觸發父組件中的事件        this.$emit('getChildData',val)      }    }  })  Vue.component('A',{    template:`      <div>        <p>this is parent compoent!</p>        <B :messagec="messagec" :message="message" v-on:getCData="getCData" v-on:getChildData="getChildData(message)"></B>      </div>    `,    data(){      return {        message:'hello',        messagec:'hello c' //傳遞給c組件的數據      }    },    methods:{      getChildData(val){        console.log('這是來自B組件的數據')      },      //執行C子組件觸發的事件      getCData(val){        console.log("這是來自C組件的數據:"+val)      }    }  })  var app=new Vue({    el:'#app',    template:`      <div>        <A></A>      </div>    `  })

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

父組件中通過provider來提供變量,然后在子組件中通過inject來注入變量。不論子組件有多深,只要調用了inject那么就可以注入provider中的數據。而不是局限于只能從當前父組件的prop屬性來獲取數據,只要在父組件的生命周期內,子組件都可以調用。

Vue.component('child',{    inject:['for'],//得到父組件傳遞過來的數據    data(){      return {        mymessage:this.for      }    },    template:`      <div>        <input type="tet" v-model="mymessage">      </div>  })  Vue.component('parent',{    template:`      <div>        <p>this is parent compoent!</p>        <child></child>      </div>    `,    provide:{      for:'test'    },    data(){      return {        message:'hello'      }    }  })  var app=new Vue({    el:'#app',    template:`      <div>        <parent></parent>      </div>    `  })

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

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. boradcast和dispatch

vue1.0中提供了這種方式,但vue2.0中沒有,但很多開源軟件都自己封裝了這種方式,比如min ui、element ui和iview等。

比如如下代碼,一般都作為一個mixins去使用, broadcast是向特定的父組件,觸發事件,dispatch是向特定的子組件觸發事件,本質上這種方式還是on和on和emit的封裝,但在一些基礎組件中卻很實用。

function broadcast(componentName, eventName, params) { this.$children.forEach(child => {  var name = child.$options.componentName;  if (name === componentName) {   child.$emit.apply(child, [eventName].concat(params));  } else {   broadcast.apply(child, [componentName, eventName].concat(params));  } });}export default { methods: {  dispatch(componentName, eventName, params) {   var parent = this.$parent;   var name = parent.$options.componentName;   while (parent && (!name || name !== componentName)) {    parent = parent.$parent;    if (parent) {     name = parent.$options.componentName;    }   }   if (parent) {    parent.$emit.apply(parent, [eventName].concat(params));   }  },  broadcast(componentName, eventName, params) {   broadcast.call(this, componentName, eventName, params);  } }};

8. vuex處理組件之間的數據交互

如果業務邏輯復雜,很多組件之間需要同時處理一些公共的數據,這個時候才有上面這一些方法可能不利于項目的維護,vuex的做法就是將這一些公共的數據抽離出來,然后其他組件就可以對這個公共數據進行讀寫操作,這樣達到了解耦的目的。

詳情可參考:https://vuex.vuejs.org/zh-cn/

希望本文所述對大家vue.js程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 白水县| 大余县| 青河县| 六安市| 昌邑市| 宁武县| 麻江县| 陕西省| 雷州市| 故城县| 永嘉县| 泸州市| 祁连县| 铁力市| 九江市| 福清市| 赤壁市| 公安县| 柳江县| 永仁县| 昌邑市| 革吉县| 周口市| 东丽区| 万安县| 保靖县| 沙坪坝区| 顺昌县| 西藏| 绥滨县| 龙海市| 蒙山县| 铜梁县| 崇义县| 迁安市| 玛多县| 鸡东县| 大新县| 渭南市| 浮山县| 潞城市|