欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue中$emit傳遞多個(gè)參(arguments和$event)

 更新時(shí)間:2023年02月02日 09:18:44   作者:清風(fēng)細(xì)雨_林木木  
本文主要介紹了vue中$emit傳遞多個(gè)參(arguments和$event),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言

使用 $emit 從子組件傳遞數(shù)據(jù)到父組件時(shí),主要有以下3類情況

1.只有子組件傳值(單個(gè)、多個(gè))

寫(xiě)法一:(自由式)

// child組件,在子組件中觸發(fā)事件
this.$emit('handleFather', '子參數(shù)1','子參數(shù)2','子參數(shù)3')
// father組件,在父組件中引用子組件
<child @handleFather="handleFather"></child>
<script>
export default {
   components: {
    child,
   }
   methods: {
     handleFather(param1,param2,param3) {
         console.log(param) // 
     }
   }
 }
 </script>

解析:

  • 只有子組件傳值;
  • 注意@引用函數(shù)不需要加“括號(hào)”;
  • 子組件傳值和父組件方法的參數(shù)一一對(duì)應(yīng)。

寫(xiě)法二:(arguments寫(xiě)法)

// child組件,在子組件中觸發(fā)事件并傳多個(gè)參數(shù)
this.$emit('handleFather', param1, param2,)
//father組件,在父組件中引用子組件
<child @handleFather="handleFather(arguments)"></child>
<script>
  export default {
    components: {
     child,
    }
    methods: {
      handleFather(param) {
          console.log(param[0]) //獲取param1的值
          console.log(param[1]) //獲取param2的值
      }
    }
  }
</script>

解析:

  • 只有子組件傳值;
  • 注意@引用函數(shù)添加“arguments”值;
  • 打印出子組件傳值的數(shù)組形式。

2.子組件傳值,父組件也傳值

寫(xiě)法一:

// child組件,在子組件中觸發(fā)事件
this.$emit('handleFather', '子參數(shù)對(duì)象')
//father組件,在父組件中引用子組件
<child @handleFather="handleFather($event, fatherParam)"></child>
 
<script>
 export default {
   components: {
    child,
   }
   methods: {
     handleFather(childObj, fatherParam) {
         console.log(childObj) // 打印子組件參數(shù)(對(duì)象)
         console.log(fatherParam) // 父組件參數(shù)
     }
   }
 }
</script>

寫(xiě)法二:

// child組件,在子組件中觸發(fā)事件并傳多個(gè)參數(shù)
this.$emit('handleFather', param1, param2,)
//father組件,在父組件中引用子組件
<child @handleFather="handleFather(arguments, fatherParam)"></child>

<script>
?export default {
? ?components: {
? ? child,
? ?}
? ?methods: {
? ? ?handleFather(childParam, fatherParam) {
? ? ? ? ?console.log(childParam) //獲取arguments數(shù)組參數(shù)
? ? ? ? ?console.log(fatherParam) //獲取fatherParam
? ? ?}
? ?}
?}
</script>

總結(jié):

  • 只有子組件傳參,@調(diào)用方法不使用“括號(hào)”
  • 特殊使用“arguments”和“$event”,
  • arguments 獲取子參數(shù)的數(shù)組
  • $event 獲取自定義對(duì)象,滿足傳多個(gè)參數(shù)

到此這篇關(guān)于vue中$emit傳遞多個(gè)參(arguments和$event)的文章就介紹到這了,更多相關(guān)vue $emit傳遞多個(gè)參內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評(píng)論