久久精品五月,日韩不卡视频在线观看,国产精品videossex久久发布 ,久久av综合

站長(zhǎng)資訊網(wǎng)
最全最豐富的資訊網(wǎng)站

組件間怎么通信?盤點(diǎn)Vue組件通信方式(值得收藏)

Vue組件間怎么通信?本篇文章盤點(diǎn)Vue2和Vue3的10種組件通信方式,希望對(duì)大家有所幫助!

組件間怎么通信?盤點(diǎn)Vue組件通信方式(值得收藏)

Vue中組件通信方式有很多,其中Vue2和Vue3實(shí)現(xiàn)起來也會(huì)有很多差異;本文將通過選項(xiàng)式API 組合式API以及setup三種不同實(shí)現(xiàn)方式全面介紹Vue2和Vue3的組件通信方式。其中將要實(shí)現(xiàn)的通信方式如下表所示。(學(xué)習(xí)視頻分享:vue視頻教程)

方式 Vue2 Vue3
父?jìng)髯?/td> props props
子傳父 $emit emits
父?jìng)髯?/td> $attrs attrs
子傳父 $listeners 無(合并到 attrs方式)
父?jìng)髯?/td> provide provide
子傳父 inject inject
子組件訪問父組件 $parent
父組件訪問子組件 $children
父組件訪問子組件 $ref expose&ref
兄弟傳值 EventBus mitt

props

props是組件通信中最常用的通信方式之一。父組件通過v-bind傳入,子組件通過props接收,下面是它的三種實(shí)現(xiàn)方式

  • 選項(xiàng)式API
//父組件  <template>   <div>     <Child :msg="parentMsg" />   </div> </template> <script> import Child from './Child' export default {   components:{     Child   },   data() {     return {       parentMsg: '父組件信息'     }   } } </script>   //子組件  <template>   <div>     {{msg}}   </div> </template> <script> export default {   props:['msg'] } </script>
  • 組合式Api
//父組件  <template>   <div>     <Child :msg="parentMsg" />   </div> </template> <script> import { ref,defineComponent } from 'vue' import Child from './Child.vue' export default defineComponent({   components:{     Child   },   setup() {     const parentMsg = ref('父組件信息')     return {       parentMsg     };   }, }); </script>  //子組件  <template>     <div>         {{ parentMsg }}     </div> </template> <script> import { defineComponent,toRef } from "vue"; export default defineComponent({     props: ["msg"],// 如果這行不寫,下面就接收不到     setup(props) {         console.log(props.msg) //父組件信息         let parentMsg = toRef(props, 'msg')         return {             parentMsg         };     }, }); </script>
  • setup語法糖
//父組件  <template>   <div>     <Child :msg="parentMsg" />   </div> </template> <script setup> import { ref } from 'vue' import Child from './Child.vue' const parentMsg = ref('父組件信息') </script>  //子組件  <template>     <div>         {{ parentMsg }}     </div> </template> <script setup> import { toRef, defineProps } from "vue"; const props = defineProps(["msg"]); console.log(props.msg) //父組件信息 let parentMsg = toRef(props, 'msg') </script>

注意

props中數(shù)據(jù)流是單項(xiàng)的,即子組件不可改變父組件傳來的值

在組合式API中,如果想在子組件中用其它變量接收props的值時(shí)需要使用toRef將props中的屬性轉(zhuǎn)為響應(yīng)式。

emit

子組件可以通過emit發(fā)布一個(gè)事件并傳遞一些參數(shù),父組件通過v-on進(jìn)行這個(gè)事件的監(jiān)聽

  • 選項(xiàng)式API
//父組件  <template>   <div>     <Child @sendMsg="getFromChild" />   </div> </template> <script> import Child from './Child' export default {   components:{     Child   },   methods: {     getFromChild(val) {       console.log(val) //我是子組件數(shù)據(jù)     }   } } </script>  // 子組件  <template>   <div>     <button @click="sendFun">send</button>   </div> </template> <script> export default {   methods:{     sendFun(){       this.$emit('sendMsg','我是子組件數(shù)據(jù)')     }   } } </script>
  • 組合式Api
//父組件  <template>   <div>     <Child @sendMsg="getFromChild" />   </div> </template> <script> import Child from './Child' import { defineComponent } from "vue"; export default defineComponent({   components: {     Child   },   setup() {     const getFromChild = (val) => {       console.log(val) //我是子組件數(shù)據(jù)     }     return {       getFromChild     };   }, }); </script>  //子組件  <template>     <div>         <button @click="sendFun">send</button>     </div> </template>  <script> import { defineComponent } from "vue"; export default defineComponent({     emits: ['sendMsg'],     setup(props, ctx) {         const sendFun = () => {             ctx.emit('sendMsg', '我是子組件數(shù)據(jù)')         }         return {             sendFun         };     }, }); </script>
  • setup語法糖
//父組件  <template>   <div>     <Child @sendMsg="getFromChild" />   </div> </template> <script setup> import Child from './Child' const getFromChild = (val) => {       console.log(val) //我是子組件數(shù)據(jù)     } </script>  //子組件  <template>     <div>         <button @click="sendFun">send</button>     </div> </template> <script setup> import { defineEmits } from "vue"; const emits = defineEmits(['sendMsg']) const sendFun = () => {     emits('sendMsg', '我是子組件數(shù)據(jù)') } </script>

attrs和listeners

子組件使用$attrs可以獲得父組件除了props傳遞的屬性和特性綁定屬性 (class和 style)之外的所有屬性。

子組件使用$listeners可以獲得父組件(不含.native修飾器的)所有v-on事件監(jiān)聽器,在Vue3中已經(jīng)不再使用;但是Vue3中的attrs不僅可以獲得父組件傳來的屬性也可以獲得父組件v-on事件監(jiān)聽器

  • 選項(xiàng)式API
//父組件  <template>   <div>     <Child @parentFun="parentFun" :msg1="msg1" :msg2="msg2"  />   </div> </template> <script> import Child from './Child' export default {   components:{     Child   },   data(){     return {       msg1:'子組件msg1',       msg2:'子組件msg2'     }   },   methods: {     parentFun(val) {       console.log(`父組件方法被調(diào)用,獲得子組件傳值:${val}`)     }   } } </script>  //子組件  <template>   <div>     <button @click="getParentFun">調(diào)用父組件方法</button>   </div> </template> <script> export default {   methods:{     getParentFun(){       this.$listeners.parentFun('我是子組件數(shù)據(jù)')     }   },   created(){     //獲取父組件中所有綁定屬性     console.log(this.$attrs)  //{"msg1": "子組件msg1","msg2": "子組件msg2"}     //獲取父組件中所有綁定方法         console.log(this.$listeners) //{parentFun:f}   } } </script>
  • 組合式API
//父組件  <template>   <div>     <Child @parentFun="parentFun" :msg1="msg1" :msg2="msg2" />   </div> </template> <script> import Child from './Child' import { defineComponent,ref } from "vue"; export default defineComponent({   components: {     Child   },   setup() {     const msg1 = ref('子組件msg1')     const msg2 = ref('子組件msg2')     const parentFun = (val) => {       console.log(`父組件方法被調(diào)用,獲得子組件傳值:${val}`)     }     return {       parentFun,       msg1,       msg2     };   }, }); </script>  //子組件  <template>     <div>         <button @click="getParentFun">調(diào)用父組件方法</button>     </div> </template> <script> import { defineComponent } from "vue"; export default defineComponent({     emits: ['sendMsg'],     setup(props, ctx) {         //獲取父組件方法和事件         console.log(ctx.attrs) //Proxy {"msg1": "子組件msg1","msg2": "子組件msg2"}         const getParentFun = () => {             //調(diào)用父組件方法             ctx.attrs.onParentFun('我是子組件數(shù)據(jù)')         }         return {             getParentFun         };     }, }); </script>
  • setup語法糖
//父組件  <template>   <div>     <Child @parentFun="parentFun" :msg1="msg1" :msg2="msg2" />   </div> </template> <script setup> import Child from './Child' import { ref } from "vue"; const msg1 = ref('子組件msg1') const msg2 = ref('子組件msg2') const parentFun = (val) => {   console.log(`父組件方法被調(diào)用,獲得子組件傳值:${val}`) } </script>  //子組件  <template>     <div>         <button @click="getParentFun">調(diào)用父組件方法</button>     </div> </template> <script setup> import { useAttrs } from "vue";  const attrs = useAttrs() //獲取父組件方法和事件 console.log(attrs) //Proxy {"msg1": "子組件msg1","msg2": "子組件msg2"} const getParentFun = () => {     //調(diào)用父組件方法     attrs.onParentFun('我是子組件數(shù)據(jù)') } </script>

注意

Vue3中使用attrs調(diào)用父組件方法時(shí),方法前需要加上on;如parentFun->onParentFun

provide/inject

provide:是一個(gè)對(duì)象,或者是一個(gè)返回對(duì)象的函數(shù)。里面包含要給子孫后代屬性

inject:一個(gè)字符串?dāng)?shù)組,或者是一個(gè)對(duì)象。獲取父組件或更高層次的組件provide的值,既在任何后代組件都可以通過inject獲得

  • 選項(xiàng)式API
//父組件 <script> import Child from './Child' export default {   components: {     Child   },   data() {     return {       msg1: '子組件msg1',       msg2: '子組件msg2'     }   },   provide() {     return {       msg1: this.msg1,       msg2: this.msg2     }   } } </script>  //子組件  <script> export default {   inject:['msg1','msg2'],   created(){     //獲取高層級(jí)提供的屬性     console.log(this.msg1) //子組件msg1     console.log(this.msg2) //子組件msg2   } } </script>
  • 組合式API
//父組件  <script> import Child from './Child' import { ref, defineComponent,provide } from "vue"; export default defineComponent({   components:{     Child   },   setup() {     const msg1 = ref('子組件msg1')     const msg2 = ref('子組件msg2')     provide("msg1", msg1)     provide("msg2", msg2)     return {            }   }, }); </script>  //子組件  <template>     <div>         <button @click="getParentFun">調(diào)用父組件方法</button>     </div> </template> <script> import { inject, defineComponent } from "vue"; export default defineComponent({     setup() {         console.log(inject('msg1').value) //子組件msg1         console.log(inject('msg2').value) //子組件msg2     }, }); </script>
  • setup語法糖
//父組件 <script setup> import Child from './Child' import { ref,provide } from "vue"; const msg1 = ref('子組件msg1') const msg2 = ref('子組件msg2') provide("msg1",msg1) provide("msg2",msg2) </script>  //子組件  <script setup> import { inject } from "vue"; console.log(inject('msg1').value) //子組件msg1 console.log(inject('msg2').value) //子組件msg2 </script>

說明

provide/inject一般在深層組件嵌套中使用合適。一般在組件開發(fā)中用的居多。

parent/children

$parent: 子組件獲取父組件Vue實(shí)例,可以獲取父組件的屬性方法等

$children: 父組件獲取子組件Vue實(shí)例,是一個(gè)數(shù)組,是直接兒子的集合,但并不保證子組件的順序

  • Vue2
import Child from './Child' export default {   components: {     Child   },   created(){     console.log(this.$children) //[Child實(shí)例]     console.log(this.$parent)//父組件實(shí)例   } }

注意父組件獲取到的$children并不是響應(yīng)式的

expose&ref

$refs可以直接獲取元素屬性,同時(shí)也可以直接獲取子組件實(shí)例

  • 選項(xiàng)式API
//父組件  <template>   <div>     <Child ref="child" />   </div> </template> <script> import Child from './Child' export default {   components: {     Child   },   mounted(){     //獲取子組件屬性     console.log(this.$refs.child.msg) //子組件元素      //調(diào)用子組件方法     this.$refs.child.childFun('父組件信息')   } } </script>  //子組件   <template>   <div>     <div></div>   </div> </template> <script> export default {   data(){     return {       msg:'子組件元素'     }   },   methods:{     childFun(val){       console.log(`子組件方法被調(diào)用,值${val}`)     }   } } </script>
  • 組合式API
//父組件  <template>   <div>     <Child ref="child" />   </div> </template> <script> import Child from './Child' import { ref, defineComponent, onMounted } from "vue"; export default defineComponent({   components: {     Child   },    setup() {     const child = ref() //注意命名需要和template中ref對(duì)應(yīng)     onMounted(() => {       //獲取子組件屬性       console.log(child.value.msg) //子組件元素        //調(diào)用子組件方法       child.value.childFun('父組件信息')     })     return {       child //必須return出去 否則獲取不到實(shí)例     }   }, }); </script>  //子組件  <template>     <div>     </div> </template> <script> import { defineComponent, ref } from "vue"; export default defineComponent({     setup() {         const msg = ref('子組件元素')         const childFun = (val) => {             console.log(`子組件方法被調(diào)用,值${val}`)         }         return {             msg,             childFun         }     }, }); </script>
  • setup語法糖
//父組件  <template>   <div>     <Child ref="child" />   </div> </template> <script setup> import Child from './Child' import { ref, onMounted } from "vue"; const child = ref() //注意命名需要和template中ref對(duì)應(yīng) onMounted(() => {   //獲取子組件屬性   console.log(child.value.msg) //子組件元素    //調(diào)用子組件方法   child.value.childFun('父組件信息') }) </script>  //子組件  <template>     <div>     </div> </template> <script setup> import { ref,defineExpose } from "vue"; const msg = ref('子組件元素') const childFun = (val) => {     console.log(`子組件方法被調(diào)用,值${val}`) } //必須暴露出去父組件才會(huì)獲取到 defineExpose({     childFun,     msg }) </script>

注意

通過ref獲取子組件實(shí)例必須在頁面掛載完成后才能獲取。

在使用setup語法糖時(shí)候,子組件必須元素或方法暴露出去父組件才能獲取到

EventBus/mitt

兄弟組件通信可以通過一個(gè)事件中心EventBus實(shí)現(xiàn),既新建一個(gè)Vue實(shí)例來進(jìn)行事件的監(jiān)聽,觸發(fā)和銷毀。

在Vue3中沒有了EventBus兄弟組件通信,但是現(xiàn)在有了一個(gè)替代的方案mitt.js,原理還是 EventBus

  • 選項(xiàng)式API
//組件1 <template>   <div>     <button @click="sendMsg">傳值</button>   </div> </template> <script> import Bus from './bus.js' export default {   data(){     return {       msg:'子組件元素'     }   },   methods:{     sendMsg(){       Bus.$emit('sendMsg','兄弟的值')     }   } } </script>  //組件2  <template>   <div>     組件2   </div> </template> <script> import Bus from './bus.js' export default {   created(){    Bus.$on('sendMsg',(val)=>{     console.log(val);//兄弟的值    })   } } </script>  //bus.js  import Vue from "vue" export default new Vue()
  • 組合式API

首先安裝mitt

npm i mitt -S

然后像Vue2中bus.js一樣新建mitt.js文件

mitt.js

import mitt from 'mitt' const Mitt = mitt() export default Mitt
//組件1 <template>      <button @click="sendMsg">傳值</button> </template> <script> import { defineComponent } from "vue"; import Mitt from './mitt.js' export default defineComponent({     setup() {         const sendMsg = () => {             Mitt.emit('sendMsg','兄弟的值')         }         return {            sendMsg         }     }, }); </script>  //組件2 <template>   <div>     組件2   </div> </template> <script> import { defineComponent, onUnmounted } from "vue"; import Mitt from './mitt.js' export default defineComponent({   setup() {     const getMsg = (val) => {       console.log(val);//兄弟的值     }     Mitt.on('sendMsg', getMsg)     onUnmounted(() => {       //組件銷毀 移除監(jiān)聽       Mitt.off('sendMsg', getMsg)     })    }, }); </script>
  • setup語法糖
//組件1  <template>     <button @click="sendMsg">傳值</button> </template> <script setup> import Mitt from './mitt.js' const sendMsg = () => {     Mitt.emit('sendMsg', '兄弟的值') } </script>  //組件2  <template>   <div>     組件2   </div> </template> <script setup> import { onUnmounted } from "vue"; import Mitt from './mitt.js' const getMsg = (val) => {   console.log(val);//兄弟的值 } Mitt.on('sendMsg', getMsg) onUnmounted(() => {   //組件銷毀 移除監(jiān)聽   Mitt.off('sendMsg', getMsg) }) </script>

寫在最后

其實(shí)組件還可以借助Vuex或者Pinia狀態(tài)管理工具進(jìn)行通信(但是組件之間的通信一般不建議這樣做,因?yàn)檫@樣就會(huì)出現(xiàn)組件不能復(fù)用的問題)。對(duì)于Vuex和Pinia的用法大家可以參考這篇文章一文解析Pinia和Vuex

(學(xué)習(xí)視頻分享:web前端開發(fā)、編程基礎(chǔ)視頻)

贊(0)
分享到: 更多 (0)
?
網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)
久久精品五月,日韩不卡视频在线观看,国产精品videossex久久发布 ,久久av综合
秋霞影院一区二区三区| 性欧美精品高清| 亚洲区国产区| 中文字幕免费精品| 视频一区视频二区在线观看| 视频一区二区中文字幕| 日韩专区在线视频| 亚洲免费毛片| 日韩av成人高清| 日韩精品视频一区二区三区| 91精品视频一区二区| 国产精品天天看天天狠| 青青草精品视频| 国产精品免费不| 精品国产乱码久久久久久1区2匹| 国产精品久久久久久久久久10秀 | 国产99久久| 欧美福利在线| 爽爽淫人综合网网站| 日本午夜精品久久久久| 欧美国产另类| 日韩国产欧美| 日韩亚洲国产欧美| 丝袜美腿一区二区三区| 日韩1区2区日韩1区2区| 国产精品99久久久久久董美香| 欧美国产视频| 久久九九国产| 中文字幕亚洲在线观看| 国产精久久一区二区| 特黄特色欧美大片| 蜜臀a∨国产成人精品| 69堂精品视频在线播放| 精品国产a一区二区三区v免费| 久久精品av| 亚洲在线久久| 久久久亚洲欧洲日产| 日韩久久精品| 一本色道精品久久一区二区三区| 热久久免费视频| 久久国产乱子精品免费女| 都市激情国产精品| 丝袜美腿高跟呻吟高潮一区| 国产精久久久| 欧美午夜不卡| 国产欧美成人| 激情综合在线| 国产精品伊人| 黑丝一区二区三区| 国产伦精品一区二区三区视频| 超级白嫩亚洲国产第一| 蜜臀av在线播放一区二区三区| 国产精品jk白丝蜜臀av小说| www成人在线视频| 日韩午夜视频在线| 性感美女一区二区在线观看| 亚洲狼人精品一区二区三区| 国产91在线播放精品| 伊人久久婷婷| 国产66精品| 亚洲精一区二区三区| 国产精品不卡| 欧美一区成人| 亚洲国产一区二区在线观看| 国产精品777777在线播放| 香蕉精品999视频一区二区| 精品成av人一区二区三区| 免费成人在线视频观看| 福利一区和二区| 久久亚洲电影| 欧美黄色网页| 国产精品www.| 亚洲男人在线| 国精品一区二区三区| 久久99影视| 亚洲1区在线观看| 欧美日一区二区| 久久麻豆视频| 青青草伊人久久| 蜜芽一区二区三区| 免费成人网www| 成人在线观看免费视频| 日韩1区2区日韩1区2区| 国产婷婷精品| 国产一区二区色噜噜| 午夜精品福利影院| 亚洲精品一区二区妖精| 日韩深夜视频| 精品亚洲精品| 日本视频在线一区| 中文精品在线| 亚洲韩日在线| 日韩精品诱惑一区?区三区| 国产精品探花在线观看| 天海翼亚洲一区二区三区| 国产精品日韩| 一区视频在线| 欧美另类综合| 国内精品福利| 成人亚洲一区| 日韩成人精品一区二区| 国产精品久久| 国产精品自在| 日韩**一区毛片| 中文在线日韩| 亚洲精品国产精品粉嫩| 免费成人在线视频观看| 久色成人在线| 免费看黄色91| 久久香蕉精品| 日韩精品一卡二卡三卡四卡无卡| 亚洲女同一区| 欧美日韩国产一区精品一区| 午夜欧美理论片| 久久精品91| 欧美日韩中文一区二区| 亚洲精品.com| 久久久水蜜桃av免费网站| 99久久夜色精品国产亚洲1000部| 久久九九精品| 伊人精品在线| 免费在线观看视频一区| 亚洲一二av| 日韩国产一区二| 国产精品一区二区av交换| 国产精品网在线观看| 国产精品igao视频网网址不卡日韩| 欧美黑人巨大videos精品| 麻豆视频久久| 精品淫伦v久久水蜜桃| 国产一区二区三区网| 国产aa精品| 成人羞羞视频播放网站| 国内精品福利| 视频精品一区二区| 亚洲欧洲日韩精品在线| 欧美日韩99| 美女国产精品久久久| а√天堂中文在线资源8| 精品日韩视频| 黄色亚洲免费| 日韩国产成人精品| 国产精品对白久久久久粗| 精品中文在线| 国产一区二区三区成人欧美日韩在线观看| 日韩a一区二区| 久久精品国产99久久| 亚洲一区日韩| 日韩国产91| 国产一区二区三区国产精品| 久久精品国语| 亚洲一级淫片| 精品资源在线| 91精品91| 国产亚洲电影| 国产黄大片在线观看| 欧美在线影院| 日韩不卡在线观看日韩不卡视频 | 久久xxxx精品视频| 日韩国产在线观看| 精品久久99| 伊人精品视频| 国产亚洲精品美女久久久久久久久久| 国产一区二区精品福利地址| 欧美91视频| 亚洲美女91| 四虎8848精品成人免费网站| 精品在线91| 国产日产高清欧美一区二区三区 | 日本a级不卡| 日韩综合一区| 美女91精品| 精品一区av| 巨乳诱惑日韩免费av| 国产高清日韩| 五月婷婷亚洲| 国产精品第一国产精品| 99久久婷婷| 国产三级精品三级在线观看国产| 国产高清不卡| 最新国产精品久久久| www.九色在线| 国产农村妇女精品一二区| 欧美激情aⅴ一区二区三区| 香蕉精品视频在线观看| 国产精品久久久免费| 国产亚洲高清视频| 97人人精品| 欧美一区二区三区免费看| 激情视频一区二区三区| 国产精品日本一区二区三区在线 | 日韩av有码| 亚洲精品影院在线观看| 亚洲精品永久免费视频| 亚洲欧美日韩视频二区| av中文字幕在线观看第一页| 亚州欧美在线| 欧美综合另类| 欧美激情久久久久久久久久久| 美日韩精品视频|