vue中$emit傳遞多個(gè)參(arguments和$event)
前言
使用 $emit 從子組件傳遞數(shù)據(jù)到父組件時(shí),主要有以下3類情況
1.只有子組件傳值(單個(gè)、多個(gè))
寫法一:(自由式)
// 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ù)一一對應(yīng)。
寫法二:(arguments寫法)
// 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.子組件傳值,父組件也傳值
寫法一:
// 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ā)事件并傳多個(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)用方法不使用“括號”
- 特殊使用“arguments”和“$event”,
- arguments 獲取子參數(shù)的數(shù)組
- $event 獲取自定義對象,滿足傳多個(gè)參數(shù)
到此這篇關(guān)于vue中$emit傳遞多個(gè)參(arguments和$event)的文章就介紹到這了,更多相關(guān)vue $emit傳遞多個(gè)參內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue v-model實(shí)現(xiàn)自定義樣式多選與單選功能
這篇文章主要介紹了vue v-model實(shí)現(xiàn)自定義樣式多選與單選功能所遇到的問題,本文給大家?guī)砹私鉀Q思路和實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-07-07
Vue解析剪切板圖片并實(shí)現(xiàn)發(fā)送功能
這篇文章主要介紹了Vue解析剪切板圖片并實(shí)現(xiàn)發(fā)送功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
vue $nextTick實(shí)現(xiàn)原理深入詳解
這篇文章主要介紹了vue $nextTick實(shí)現(xiàn)原理深入詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
vue3語法中使用vscode打開滿屏紅線報(bào)錯(cuò)的完美解決方法
這篇文章主要介紹了vue3語法中使用vscode打開滿屏紅線報(bào)錯(cuò)的完美解決方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06
Vue2.0使用嵌套路由實(shí)現(xiàn)頁面內(nèi)容切換/公用一級菜單控制頁面內(nèi)容切換(推薦)
這篇文章主要介紹了Vue2.0使用嵌套路由實(shí)現(xiàn)頁面內(nèi)容切換/公用一級菜單控制頁面內(nèi)容切換,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05

