vue中$emit傳遞多個參(arguments和$event)
更新時間:2023年02月02日 09:18:44 作者:清風細雨_林木木
本文主要介紹了vue中$emit傳遞多個參(arguments和$event),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
前言
使用 $emit 從子組件傳遞數(shù)據(jù)到父組件時,主要有以下3類情況
1.只有子組件傳值(單個、多個)
寫法一:(自由式)
// 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ù)不需要加“括號”;
- 子組件傳值和父組件方法的參數(shù)一一對應。
寫法二:(arguments寫法)
// child組件,在子組件中觸發(fā)事件并傳多個參數(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.子組件傳值,父組件也傳值
寫法一:
// child組件,在子組件中觸發(fā)事件
this.$emit('handleFather', '子參數(shù)對象')
//father組件,在父組件中引用子組件
<child @handleFather="handleFather($event, fatherParam)"></child>
<script>
export default {
components: {
child,
}
methods: {
handleFather(childObj, fatherParam) {
console.log(childObj) // 打印子組件參數(shù)(對象)
console.log(fatherParam) // 父組件參數(shù)
}
}
}
</script>
寫法二:
// child組件,在子組件中觸發(fā)事件并傳多個參數(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>總結:
- 只有子組件傳參,@調用方法不使用“括號”
- 特殊使用“arguments”和“$event”,
- arguments 獲取子參數(shù)的數(shù)組
- $event 獲取自定義對象,滿足傳多個參數(shù)
到此這篇關于vue中$emit傳遞多個參(arguments和$event)的文章就介紹到這了,更多相關vue $emit傳遞多個參內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue v-model實現(xiàn)自定義樣式多選與單選功能
這篇文章主要介紹了vue v-model實現(xiàn)自定義樣式多選與單選功能所遇到的問題,本文給大家?guī)砹私鉀Q思路和實現(xiàn)代碼,需要的朋友可以參考下2018-07-07
vue3語法中使用vscode打開滿屏紅線報錯的完美解決方法
這篇文章主要介紹了vue3語法中使用vscode打開滿屏紅線報錯的完美解決方法,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
Vue2.0使用嵌套路由實現(xiàn)頁面內容切換/公用一級菜單控制頁面內容切換(推薦)
這篇文章主要介紹了Vue2.0使用嵌套路由實現(xiàn)頁面內容切換/公用一級菜單控制頁面內容切換,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05

