how to remove deprecated $listeners in vue 3

Viewed 2729

In migration from vue 2 to vue 3, i am getting some compilation warnings. Deprecation of $listeners in components is one of those warnings. I have checked official documentation to use $attrs by removing $listeners. I am new in vue 3. So, not able to understand how to handle those warnings related to listeners.

Here is the snippet: 1st case: Component 1

    <template>
    <div>
        <input ref="input"
               :value="txtField"
               @input="txtField=$event.target.value"
               :type="inputType"
               :class="inputClass"
               :placeholder="placeholder"
               :disabled="disabled"
               :readonly="readonly"
               :onfocus="disabled&&'this.blur();'"
               :tabindex="tabindex"
               v-on="listenersInput" // here is the method where $listeners used
               @keyup.enter="enterHandler"
               @blur="validateOnEvent"/>
     </div>
</template>

//method 

listenersInput() {
            //var vm = this;
            return Object.assign({}, this.$listeners, {
                input: function(event){ /*vm.$emit('input',event.target.value);*/}
            });
        },

2nd case: Component 2

    <template>
    <custom-button v-bind="buttonProps"
                 v-on="$listeners"
                 :class="buttonClass"
                 @click="tooggle"></custom-button>
</template>

How to handle these two cases?

2 Answers

In Vue 2, you could bind all event listeners using v-on="$listeners".

In Vue 3, according to the documentation, if you want to apply fallthrough attributes to a non-root element, you have to use v-bind="$attrs" which also includes the event listeners.

So this Vue 2 code:

<div>
  <my-button v-on="$listeners">click me</my-button>
</div>

Looks like this in Vue 3:

<div>
  <my-button v-bind="$attrs">click me</my-button>
</div>
Related