VUE父组件监听$emit事件,如何传递多个父组件自己的参数
背景
子组件可以通过this.$emit(‘change’, ‘parm’:‘value1’,‘parm2’:‘value2’…)传递多个参数
父组件监听事件传参有两种方式
方式1:父组件自己无参数
方法名可以不用带参数,函数中的e代表change事件的对象,直接获取参数即可
<uni-rate value="5" @change="clickStar" :activeColor="star[index[0]].color" size="25.5" margin="6.5"
clickStar(e){
this.$set(this.index,e.value)
}
方式2:父组件传递自己的参数
父元素引入了多个组件,我们想区分具体是哪个组件触发了change事件
<uni-rate value="5" @change="clickStar($event,0)" :activeColor="star[index[0]].color" size="25.5" margin="6.5" style="margin:30upx 0upx;">星星</uni-rate>
<uni-rate value="5" @change="clickStar($event,1)" :activeColor="star[index[0]].color" size="25.5" margin="6.5" style="margin:30upx 0upx;">星星</uni-rate>
<uni-rate value="5" @change="clickStar($event,2)" :activeColor="star[index[0]].color" size="25.5" margin="6.5" style="margin:30upx 0upx;">星星</uni-rate>
父组件用$event代表change事件对象,后面传递父元素自己的参数
clickStar(e,idx){
this.$set(this.index,idx,e.value-1)
}
事件中根据位置传参,e代表了事件对象,idx代表了父元素自己的第一个参数。
还没有评论,来说两句吧...