Skip to content

自定义事件传参问题 #5735

@yuu2lee4

Description

@yuu2lee4

What problem does this feature solve?

在组件内部发射的事件并且有参数传递出来的时候,如果在业务调用该组件,并监听该事件时,需要在组件外面传参进来的时候,就会覆盖组件内部传递出来的参数,就像文档事件处理器一节所讲的原生事件监听,如果自己传了参数,就会覆盖掉默认的event对象参数一样,必须要显示地传入$event,很神奇的是,在自定义事件这里,也可以传入$event来获取到组件内部传递出来的第一个参数,这个文档里并没有写到,而且从这个$event命名来看,应该是一个不小心疏忽的'礼物',但是就算有这个‘bug’存在,第二个参数,第三个参数都是无法获取的,目前的解决方法就是都放在第一个参数里面,我希望能在外面对所有参数都能显式地去使用。

What does the proposed API look like?

Parent component:

<child v-for="(item, index) in data" @custom="parentMethod(index, $1, $2)"></child>

Child component:

//someWhere emit a custom event  
......  
this.$emit('custom', arg1, arg2);  
......

这里parentMethod传入了index,$1和$2对应子组件里的arg1和arg2,在parentMethod这里,顺序不重要,并且$1和$2不是必传的,看需求。
为了清楚地表达我的意思,使用了中文,不好意思。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions