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

站長資訊網
最全最豐富的資訊網站

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

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

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

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

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

props

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

  • 選項式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中數據流是單項的,即子組件不可改變父組件傳來的值

在組合式API中,如果想在子組件中用其它變量接收props的值時需要使用toRef將props中的屬性轉為響應式。

emit

子組件可以通過emit發布一個事件并傳遞一些參數,父組件通過v-on進行這個事件的監聽

  • 選項式API
//父組件  <template>   <div>     <Child @sendMsg="getFromChild" />   </div> </template> <script> import Child from './Child' export default {   components:{     Child   },   methods: {     getFromChild(val) {       console.log(val) //我是子組件數據     }   } } </script>  // 子組件  <template>   <div>     <button @click="sendFun">send</button>   </div> </template> <script> export default {   methods:{     sendFun(){       this.$emit('sendMsg','我是子組件數據')     }   } } </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) //我是子組件數據     }     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', '我是子組件數據')         }         return {             sendFun         };     }, }); </script>
  • setup語法糖
//父組件  <template>   <div>     <Child @sendMsg="getFromChild" />   </div> </template> <script setup> import Child from './Child' const getFromChild = (val) => {       console.log(val) //我是子組件數據     } </script>  //子組件  <template>     <div>         <button @click="sendFun">send</button>     </div> </template> <script setup> import { defineEmits } from "vue"; const emits = defineEmits(['sendMsg']) const sendFun = () => {     emits('sendMsg', '我是子組件數據') } </script>

attrs和listeners

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

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

  • 選項式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(`父組件方法被調用,獲得子組件傳值:${val}`)     }   } } </script>  //子組件  <template>   <div>     <button @click="getParentFun">調用父組件方法</button>   </div> </template> <script> export default {   methods:{     getParentFun(){       this.$listeners.parentFun('我是子組件數據')     }   },   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(`父組件方法被調用,獲得子組件傳值:${val}`)     }     return {       parentFun,       msg1,       msg2     };   }, }); </script>  //子組件  <template>     <div>         <button @click="getParentFun">調用父組件方法</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 = () => {             //調用父組件方法             ctx.attrs.onParentFun('我是子組件數據')         }         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(`父組件方法被調用,獲得子組件傳值:${val}`) } </script>  //子組件  <template>     <div>         <button @click="getParentFun">調用父組件方法</button>     </div> </template> <script setup> import { useAttrs } from "vue";  const attrs = useAttrs() //獲取父組件方法和事件 console.log(attrs) //Proxy {"msg1": "子組件msg1","msg2": "子組件msg2"} const getParentFun = () => {     //調用父組件方法     attrs.onParentFun('我是子組件數據') } </script>

注意

Vue3中使用attrs調用父組件方法時,方法前需要加上on;如parentFun->onParentFun

provide/inject

provide:是一個對象,或者是一個返回對象的函數。里面包含要給子孫后代屬性

inject:一個字符串數組,或者是一個對象。獲取父組件或更高層次的組件provide的值,既在任何后代組件都可以通過inject獲得

  • 選項式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(){     //獲取高層級提供的屬性     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">調用父組件方法</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一般在深層組件嵌套中使用合適。一般在組件開發中用的居多。

parent/children

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

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

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

注意父組件獲取到的$children并不是響應式的

expose&ref

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

  • 選項式API
//父組件  <template>   <div>     <Child ref="child" />   </div> </template> <script> import Child from './Child' export default {   components: {     Child   },   mounted(){     //獲取子組件屬性     console.log(this.$refs.child.msg) //子組件元素      //調用子組件方法     this.$refs.child.childFun('父組件信息')   } } </script>  //子組件   <template>   <div>     <div></div>   </div> </template> <script> export default {   data(){     return {       msg:'子組件元素'     }   },   methods:{     childFun(val){       console.log(`子組件方法被調用,值${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對應     onMounted(() => {       //獲取子組件屬性       console.log(child.value.msg) //子組件元素        //調用子組件方法       child.value.childFun('父組件信息')     })     return {       child //必須return出去 否則獲取不到實例     }   }, }); </script>  //子組件  <template>     <div>     </div> </template> <script> import { defineComponent, ref } from "vue"; export default defineComponent({     setup() {         const msg = ref('子組件元素')         const childFun = (val) => {             console.log(`子組件方法被調用,值${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對應 onMounted(() => {   //獲取子組件屬性   console.log(child.value.msg) //子組件元素    //調用子組件方法   child.value.childFun('父組件信息') }) </script>  //子組件  <template>     <div>     </div> </template> <script setup> import { ref,defineExpose } from "vue"; const msg = ref('子組件元素') const childFun = (val) => {     console.log(`子組件方法被調用,值${val}`) } //必須暴露出去父組件才會獲取到 defineExpose({     childFun,     msg }) </script>

注意

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

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

EventBus/mitt

兄弟組件通信可以通過一個事件中心EventBus實現,既新建一個Vue實例來進行事件的監聽,觸發和銷毀。

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

  • 選項式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(() => {       //組件銷毀 移除監聽       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(() => {   //組件銷毀 移除監聽   Mitt.off('sendMsg', getMsg) }) </script>

寫在最后

其實組件還可以借助Vuex或者Pinia狀態管理工具進行通信(但是組件之間的通信一般不建議這樣做,因為這樣就會出現組件不能復用的問題)。對于Vuex和Pinia的用法大家可以參考這篇文章一文解析Pinia和Vuex

(學習視頻分享:web前端開發、編程基礎視頻)

贊(0)
分享到: 更多 (0)
?
網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
久久精品五月,日韩不卡视频在线观看,国产精品videossex久久发布 ,久久av综合
久久只有精品| 亚洲图片久久| 国产精品久久久一区二区| 综合干狼人综合首页| 欧美不卡视频| 99国内精品| 日韩精品一卡二卡三卡四卡无卡| 亚洲一区二区免费看| 99视频在线精品国自产拍免费观看| 午夜久久免费观看| 丝袜美腿一区二区三区| 只有精品亚洲| 国产精品一页| 国产一区二区三区不卡av| 97国产精品| 久久久久国产精品一区三寸| 国产一区亚洲| 亚洲一区二区小说| 国产亚洲一区二区三区啪| 国产精品久久久久久久久久妞妞 | 91精品福利| 快she精品国产999| 亚洲久久视频| 国产精品久久久一区二区| 综合日韩av| 国产亚洲精品v| 欧美日韩亚洲一区| 国产一区二区三区探花| 今天的高清视频免费播放成人| 亚洲综合电影一区二区三区| 婷婷精品久久久久久久久久不卡| 国产美女亚洲精品7777| 国产粉嫩在线观看| av一区二区高清| 综合欧美精品| 国产精东传媒成人av电影| 国产资源在线观看入口av| 不卡在线一区二区| 日本精品久久| 在线中文字幕播放| 蜜芽一区二区三区| 加勒比视频一区| 视频一区中文| 国产麻豆一区二区三区 | 久久国产精品久久久久久电车| 日韩一区二区三免费高清在线观看 | 激情欧美丁香| 日日摸夜夜添夜夜添国产精品| 国产欧美一区二区三区国产幕精品 | 亚洲伦乱视频| 亚洲香蕉视频| 色欧美自拍视频| 日韩中文字幕区一区有砖一区| 欧美极品中文字幕| 99riav1国产精品视频| 国产精品久久久久久妇女 | se01亚洲视频| 日韩久久一区| 99久久亚洲精品蜜臀| 欧美自拍一区| 免费观看久久av| 国产精品v亚洲精品v日韩精品| 99精品电影| 国产精品久久久久9999高清| 夜夜精品视频| 91一区二区| 日本久久一区| 樱桃成人精品视频在线播放| 欧美激情精品| 99成人在线| 日韩欧美一区二区三区免费观看| 日本欧美在线| 国产亚洲精品久久久久婷婷瑜伽| 成人一二三区| 青青草精品视频| 在线亚洲激情| 成人片免费看| 国产精品亚洲产品| 蜜桃免费网站一区二区三区| 亚洲电影有码| 精品国产18久久久久久二百| 日韩欧美另类中文字幕| 午夜电影亚洲| 蜜桃视频在线网站| 日韩av中文字幕一区二区| 九九久久电影| 欧美香蕉视频| 精品久久精品| 国产精品久一| 91亚洲无吗| 亚洲专区视频| 亚洲一区欧美二区| 亚洲特色特黄| 日韩精品麻豆| 91亚洲一区| 成人国产精品一区二区网站| 国产日韩欧美三级| 日韩av资源网| 日韩av一级片| 亚洲精品极品| 中文无码久久精品| 蜜臀久久久99精品久久久久久| 亚洲精品1区| 亚洲精品一区二区在线看| 久久久国产亚洲精品| 伊人久久视频| 欧产日产国产精品视频| 98精品久久久久久久| 精品国产一区二| 久久精品国产亚洲一区二区三区| 国产欧美精品久久| 97成人在线| 亚洲精品自拍| 亚洲九九精品| 蜜乳av另类精品一区二区| 99国产精品99久久久久久粉嫩| 欧美日韩高清| 午夜在线精品| 久久亚洲欧美| 蜜臀av国产精品久久久久 | 国产精品久久久久久久久久妞妞| 久久国产三级| 国产毛片精品久久| 精品久久在线| 亚洲黄色网址| 久久国产日韩| 国产精品7m凸凹视频分类| 久久久天天操| 欧美理论视频| 国产美女一区| 亚洲精品在线国产| 国产伦精品一区二区三区千人斩 | 日韩精品免费一区二区夜夜嗨| 日韩综合一区二区三区| 日本成人在线不卡视频| 88久久精品| 国产丝袜一区| 国产在视频一区二区三区吞精| 水蜜桃久久夜色精品一区| 涩涩av在线| 亚洲国产专区校园欧美| 亚洲欧美日韩综合国产aⅴ| 亚洲日产av中文字幕| 国产免费av一区二区三区| 国产一区二区三区不卡视频网站| 成人免费电影网址| 欧美日韩国产高清| 亚洲精品影院在线观看| 国产欧美成人| 色一区二区三区| 亚洲一区二区三区高清| 欧美视频久久| av免费不卡国产观看| 欧美日韩国产传媒| 日韩1区2区3区| 美女视频网站久久| 久久久夜夜夜| 日韩1区2区3区| 在线天堂中文资源最新版| 午夜一级久久| 免费日韩成人| 亚洲手机视频| 日韩精品欧美成人高清一区二区| 久久影视三级福利片| 1024精品一区二区三区| 日本伊人久久| 成人一区不卡| 久久国产精品久久久久久电车| 国产精品一区免费在线| 日韩中文在线播放| 久久最新视频| 国产精品欧美三级在线观看| 丝袜美腿一区| 日韩1区2区3区| 日韩精品dvd| 亚洲精选91| 福利在线免费视频| 亚洲乱亚洲高清| 色在线视频观看| 日本中文字幕视频一区| 丝袜诱惑一区二区| 国产精品日本| 国产一区二区三区久久久久久久久| 电影亚洲精品噜噜在线观看| 亚洲丝袜啪啪| 日韩综合精品| 美女精品在线| av资源亚洲| 青青草伊人久久| 亚洲成人免费| 麻豆精品视频在线| 免费观看在线色综合| 国产精品久久观看| 亚洲精品一二| 久久久久免费av| 久久不卡国产精品一区二区| 91久久在线| 亚洲国产欧美日本视频| 国产亚洲高清一区| 亚洲国产不卡|