Why doesn't emitting a primitive value in Vue 3 trigger the input event?

I ran into a curious issue involving Vue3 $emit. I was in the process of upgrading my custom Vue2 "form-input" component, which acts as a wrapper for displaying various types of input fields like text-input, select-options, textarea, and more.

This component used to use v-model to bind values and emitted input using "this.$emit('input', this.value)".

With the changes in Vue 3 to use update:modelValue instead of input, I made the necessary adjustments and also added an "Emits" property list to the component. Everything seemed to be functioning properly...

However, I soon noticed that Vue-multiselect 3 (an NPM package "vue-multiselect": "^3.0.0-alpha.2") was emitting a primitive value, specifically an integer, which was not triggering @input on the parent component.

form-input.vue

<input v-if="resolveType === 'text' || resolveType === 'number' || resolveType === 'tel' || resolveType === 'email'"
             v-model="localValue"
             @input="$emit('update:modelValue', $event.target.value)"
/>

<multiselect v-model="localValue"
             class="full-width-tags"
             @update:model-value="$emit('update:modelValue', $event)"
/>

// Keep in mind that multiselect's $event is an integer, as verified multiple times through console.log

...and when I'm watching for @input on the parent component, it appears that the event listener is triggered by the "text" type field but not by the multiselect. The only difference between these components is that the multiselect emits a primitive value (integer) while the input sends an event object.

Can anyone clarify why event objects and primitive values are treated differently for @input, and how can I adjust my multiselect's emit so that it triggers input events? Currently, I am emitting both "update:modelValue" AND "input" events consecutively for the multiselect, but I'm wondering if there is a simpler solution?

Answer №1

It appears that you have reviewed the Vue 3 Migration Guide and made changes to your custom component form-input to emit the update:modelValue event instead of the input event as noted in the guide.

However, why are you still listening for updates on your form-input component with @input instead of @update:modelValue?

The @input event placed on your form-input component in the parent works due to a specific change highlighted in this section

In essence, because your component emits the update:modelValue event using the emits option, any other event listener set on it will be directly bound to the root element of the component - which is the native <input> in this case.

To address this issue, simply update

<form-input @input="..." />
to
<form-input @update:modelValue="..." />

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to compile a template in AngularJS so that it utilizes a specific controller instance which has been provided?

I have a situation where I need to compile a template with a controller instance, even though the template does not have an ng-controller directive on it. My goal is to use the supplied controller instance in the compilation process. Below is the current c ...

Enhance the Material UI StepIcon by embedding real icons within the background circle

I have scoured through stack overflow but haven't come across a specific question addressing my issue. I am working on styling a Material UI Stepper component in a unique way. Most examples I've found use withStyles or makeStyles for color custom ...

Improving iframe functionality in AngularJS

I've been experimenting with AngularJS to dynamically update an iframe whenever a query is triggered. It works like this: a query is made, the embed link is fetched from the database, and then it's used in the iframe alongside the title and video ...

When I click on the delete button in the dialog box, a horizontal scroll bar appears on the page

I'm dealing with a table that includes an 'approved' column with three conditions. One of these conditions triggers the appearance of a delete button along with a confirmation dialog box made using Vuetify's dialog component. The iss ...

Can JQuery be used to identify the CSS styles applied to selected elements?

Highlight the text by selecting it and clicking a button. If the text is already highlighted, clicking the button should make the selected text return to normal. Can this be achieved using jQuery and a single button? Is it possible to identify the CSS st ...

Refining Content in Sharepoint List

I am looking to display specific items from a list in an HTML table only if they meet certain criteria. For example, if the client is equal to 'x', then I want to show the associated contact details. Currently, the code I have simply brings in c ...

What is the process for indicating the cache directory in npm5 when using the install command?

When using yarn, you can specify the cache folder with yarn --cache-folder [CACHE_FOLDER]. Is there an npm5 alternative to this? One option is to set the cache folder with a separate command: npm config set cache [CACHE_FOLDER]. However, I'm wonderin ...

What is causing this setInterval function to run repeatedly?

Below is the code snippet from my Vue application: mounted: function () { this.timer = setInterval(async () => { if (this.progress >= 1) { this.progress = 1 clearInterval(this.timer) } console.log('update& ...

Implementing specific actions based on user selections in an Oracle APEX navigation bar

Within the application's navigation bar, there is a Home item that is present on all pages. I want to implement a feature where if the user clicks on the Home item, a warning message will pop up based on the page number, alerting them that any unsaved ...

Meteor JS: How can I effectively manage the state of a unique template?

I'm currently delving into the realms of Session and reactive data sources within the Meteor JS framework. They prove to be quite useful for managing global UI states. However, I've encountered a challenge in scoping them to a specific instance o ...

Unable to show an image within the <div> element when hovering the mouse

Is it necessary to use the background style image in order to display an image on mouseover of a name? I have implemented a controller and would like the image to be replaced by text within a div tag. Below is my HTML code: <!DOCTYPE html> <html& ...

Error encountered while attempting to load image through multiple window.print() functions

I am having an issue with printing a receipt containing an image in a for loop when using the code below. The image does not load when multiple prints are done using the for loop (It works fine for a single print), but if I remove the image reference, it ...

Getting 6 shots to load simultaneously on Jribbble - how can I do it?

Hello! I'm still getting the hang of Javascript, but I've made progress in loading elements onto the page. As a beginner, this is a good start for me. Currently, I am working on a script to load 6 "shots" onto a page, but it seems to be loading ...

A guide on extracting data from a mongoose model and assigning it to a variable in a manner similar to the MVC design pattern

Below is an example of MVC framework code in PHP. I'm seeking advice on how to implement a similar process in Node.js using Mongoose. I am working with Node.js, MongoDB, and REST API development. Controller file: <?php class Myclass { public fu ...

Guide on parsing a JSON array passed from JavaScript using json_decode?

I am attempting to send JSON string encoded data to the PHP backend. In order to achieve this, I am utilizing a GET parameter with URL encoded JSON data in the form of an array similar to this: ["mystring1","mystring2"] However, when I try to decode it us ...

Clicking multiple times triggers an ajax call, resulting in duplicated data being displayed

Whenever I click on the pop-up menu to select a customer, it pulls data using an ajax call and updates selections on a form. Everything is working fine, but if I click on .lookup-cust-hide again, the JSON data (customer list) duplicates itself. Now I end u ...

Struggling with displaying Firebase data in React

I am facing an issue with rendering data fetched from Firebase onto the screen. The problem arises when I attempt to call the function that retrieves data from the database inside the componentDidMount() lifecycle method. Surprisingly, the function does no ...

Displaying JSON data with dynamic color changes in an HTML table: A comprehensive guide

Scenario: I am currently working on a project where I need to fetch data from an external json file, parse it, and then dynamically add it to an HTML table using the footable jQuery plugin. Question: I have a query regarding how to use JavaScript to parse ...

Fixing border prioritization in a table using the CSS style "border-collapse: collapse"

I have encountered a problem where certain borders take precedence over others when using the border-collapse: collapse styling in a table. Specifically, when setting the border-right on a cell, it appears above the border-left. Is there a way to adjust th ...

Unable to run JavaScript file fetched from cache API

For a web application built using pure vanilla JavaScript without utilizing service workers, I am looking to cache a JavaScript file hosted on an AWS S3 file server explicitly. The script below will be embedded in the index.html file of the application (UR ...