子組件用$emit攜帶參數(shù)向父組件傳遞事件,并且父組件在監(jiān)聽到事件的同時(shí)又需要傳入?yún)?shù)埋嵌,這種時(shí)候該怎么實(shí)現(xiàn)呢破加?
vue提供了兩種方法可以使得父組件在使用子組件的同時(shí)也傳入值。
情況1.子組件傳單個(gè)參數(shù)
子組件:
this.$emit('test','子組件參數(shù)')
父組件
@test='handleTest($event,userDefined)'
methods:{
handleTest(childParam,fatherParam){
console.log(childParam)//'子組件參數(shù)'
}
}
情況2.子組件傳多個(gè)參數(shù)
子組件:
this.$emit('test','子參1'雹嗦,'子參2')
父組件
@test='handleTest($arguments,userDefined)'
methods:{
handleTest(childParams,fatherParam){
console.log(childParams[0])//'子組件參數(shù)1'
console.log(childParams[1])//'子組件參數(shù)2'
}
}