Subscribe on changes!

add eventHandler after component mounted

avatar
Sep 18th 2020

What problem does this feature solve?

I am writing VCA called useEventListener,which can be used to addEventListener with native HTML element and $on with Vue Component(2.0 @vue/composition-api)。while in Vue 3.0, there are no $on any more and cannot add new eventHandler after component mounted(e.g. with component ref)

What does the proposed API look like?

<el-button @click="handleClick">click</el-button>,then we can use handleClick
but with
<el-button ref="buttonRef">click me</el-button>

useEventListener('click', evt => alert(1), false, buttonRef) // cannot get buttonRef.$on or set buttonRef.onClick(onXXX) in 3.0

readonly proxy can set onXXX instead of reject with warning message

avatar
Sep 18th 2020

See https://v3.vuejs.org/guide/migration/events-api.html#overview


Hi, thanks for your interest but Github issues are for bug reports and feature requests only. You can ask questions on the forum, the Discord server or StackOverflow.

avatar
Sep 18th 2020

@posva bro, I know use other event emitter instead of new Vue. It is feature, not question

buttonRef.$props.onClick = evt => alert(1) not work because of code https://github.com/vuejs/vue-next/blob/f316a332b055d3f448dc735365551d89041f1098/packages/reactivity/src/baseHandlers.ts#L200

It means if we want add eventHandler, the only way is @click in template, we cannot do the same thing when we got buttonRef in setup. any way ? But in 2.0 is OK

avatar
Sep 18th 2020

In native Dom, we can onclick="handleClick" or we can dom.addEventListener as we want

avatar
Sep 18th 2020

Have you tried something like buttonRef.$el.addEventListener('click', handleClick)?

avatar
Sep 18th 2020

@posva this is for component container listener,not component emitted listener

avatar
Sep 18th 2020

The components no longer implement that API: https://v3.vuejs.org/guide/migration/events-api.html#overview it went through an RFC https://github.com/vuejs/rfcs/pull/118

avatar
Sep 18th 2020

@posva no way to listen component emitted inside setup?very sad ...

avatar
Sep 18th 2020

it should be buttonRef.onClick = comp emitted click event

avatar
Sep 21st 2020

https://github.com/vuejs/vue-next/pull/2184 @posva Hi Bro, please review mine