The Vue.js 3 input field remains unchanged even after updating the value in the watch hook

I recently implemented a TextInput component and followed the instructions in the Vue documentation by adding the v-model directive to my component.

Here is the code snippet of my component:

<template>
    <div class="floating_input">
        <input type="text" :id="id" :name="name" :key="id" class="form_input"
               :placeholder="label" v-model="value">
        <label class="floating_label" :for="id">{{ label }}</label>
    </div>
</template>

<script setup>
    import { defineProps, defineEmits, computed } from "vue";

    const props = defineProps({
        label: {
            type: String,
            required: false
        },
        id: {
            type: String,
            required: true
        },
        name: {
            type: String,
            required: true
        },
        modelValue: String
    });
    const emit = defineEmits(['update:modelValue']);
    
    const value = computed({
        get() {
            return props.modelValue
        },
        set(value) {
            emit('update:modelValue', value)
        }
    })
</script>

In another component, I have set up a watcher to check if the value entered is not numeric, in which case it reverts back to the oldValue. However, when I enter a letter, although the logic works correctly and sets the oldValue as the currentValue, the displayed value on screen also shows the letters.

When inspecting Vue dev tools, this is what is seen:
https://i.sstatic.net/W02cI.png

And this is what appears in the browser:
https://i.sstatic.net/lrKaH.png

Answer №1

What's the point of waiting for the price when you can leverage the existing functionality?

...
<input type="number" v-model.number="value">
...

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

The entire component is not displayed in Vue

Just starting out with Vue. I'm looking to create a component that encompasses the entire page except for the header and footer Vue.component('main-block', {template: ` <section id="first-block">...</section> <secti ...

The parent node is returning a child element

Currently working on a website using Vue 3.0 and Element+ and encountering an inconsistency. There is a div displayed on the page (an array of objects) and the goal is to remove the object from the array when a button is clicked. The issue arises when som ...

What steps can be taken to ensure web resources such as scripts, images, etc are loaded from the correct location when implementing CORS support in a web application

In my local IIS, I have two asp.net mvc applications running on different ports. One is "App1" on port 1066 and the other is "App2" on port 1067. My requirement is for App2 to post data to an endpoint in App1 for validation. If the data is valid, App1 sho ...

Tips for accurately extracting values from a decoded JSON

Hello, I am posting this query because I recently encountered an issue with json encoding in PHP. When using the json_encode() function, my original JSON data gets converted to strings instead of maintaining its original variable type. For instance, let&a ...

What is the proper way to define the font slant as "slnt" in NextJS development?

My preference is to use the font style slnt -8 for Inter. When importing with a URL through SCSS, I am able to specify slnt -8 as follows: @import url("https://fonts.googleapis.com/css2?family=Inter:slnt,wght@-8,100..900&display=swap"); Unf ...

Combining PHP and Javascript for a single form submission to two different forms

Have you come across these discussions? submit a form inside another form Submitting form data to two locations using Javascript and PHP? Unfortunately, neither of them provides a satisfactory answer to the question. Allow me to paint the picture for ...

`Nextjs customizes the position of locales`

Currently implementing i18n translation in my project, the default root appears as follows: https://example.com/en/business/transaction Is it possible to customize the root to appear as: https://example.com/business/en/transacation Thank you. ...

Having difficulty getting Laravel Broadcasting to work with Soketi listening to a channel through Echo

I've integrated Laravel Broadcasting with Soketi and I'm currently attempting to subscribe to the jobs channel and listen for the JobFailedEvent. Below is a snippet of my code. class JobFailedEvent implements ShouldBroadcast { use Dispatch ...

Display open time slots in increments of 15 minutes on Fullcalendar

Currently, I am utilizing the fullcalendar plugin with the 'agendaweek' view. My goal is to showcase the available time slots as clickable and highlight the busy ones with a red background. Handling the highlighting of busy slots is not an issue ...

Using Vue.js to create dynamic routes that fetch and utilize a unique parameter ID

user/index.vue <tbody> <tr v-for="item in items" :key="item.id" > <td v-if="false" v-text="item.id" /> <td v-text="item.name" /> ...

Unfortunately, CSS3 PIE is not compatible with border-radius and box-shadow properties

I created a basic HTML/CSS code for testing purposes, but I'm having trouble getting the library to function properly. I've placed the .htc, .php and .js files in the same directory as index.html, but it just won't work. Check out the code ...

Error message stating 'compression is not defined' encountered while attempting to deploy a Node.js application on Heroku

Why is Heroku indicating that compression is undefined? Strangely, when I manually set process.env.NODE_ENV = 'production' and run the app with node server, everything works perfectly... Error log can be found here: https://gist.github.com/anony ...

Is there a way to automatically validate v-forms inside a v-data-table when the page loads?

In my data entry form, I have utilized a v-data-table with each column containing a v-form and v-text-field for direct value updates. My goal is to validate all fields upon page load to identify any incorrect data inputs. However, I am facing challenges in ...

Persisted state in Vuex fails to retain data after the page is refreshed

I recently added persisted state to my Vue application using the command npm install --save vuex-persistedstate. After that, I configured my Vuex store.js file in the following way: import Vue from 'vue' import Vuex from 'vuex' import ...

Is there a way to set an image as the background of my HTML screen?

{% extends "layout.html" %} {% block app_content %} <div> {% from "_formhelpers.html" import render_field %} <form method="post" enctype="multipart/form-data"> <div class = "container"> < ...

switch the anchor tag value and send it along with the URL

I am trying to update the value of "trans" in the Ajax URL based on a toggle between on and off values. However, despite successfully toggling the text, the URL parameter does not change accordingly. Can anyone offer any assistance? I currently have to rel ...

Encountering a missing value within an array

Within my default JSON file, I have the following structure: { "_name":"__tableframe__top", "_use-attribute-sets":"common.border__top", "__prefix":"xsl" } My goal is to add values by creating an array, but I am encountering an issue where my ...

Validation of VAT numbers using JavaScript instead of PHP

I came across an interesting function at this link that is used for checking VAT numbers in the EU. However, I am struggling to integrate it with my registration form. I would like to convert it into a JavaScript function so that I can validate the number ...

The usage of the bootstrapTable() function creates a gap below the displayed table information

Currently, I am working on incorporating a table into my webpage that will load data from an API. After some research, I found a Bootstrap table library to assist with this task. However, I have encountered an issue with setting the table height dynamicall ...

Issue: Unable to locate 'storysolo' from state 'index'

On my index.php page, I have a ui-sref link set up like this <a ui-sref="storysolo({ storyId: headline.nid })"> Additionally, my main js file is loading the angular code in the following manner var rebelFleet = angular.module("CougsApp", ["ui.ro ...