Why is the value always left unused?

I am having an issue with handling value changes on focus and blur events in my input field. Here is the code snippet:

<input
  v-model="totalAmount"
  @focus="hideSymbol(totalAmount)"
  @blur="showSymbol(totalAmount)"
/>
...
const totalAmount = ref('')

After defining functions hideSymbol and showSymbol, I encountered a problem where I received a warning message saying

The value assigned to 'val' is never used
. It seems like I missed something crucial in my learning process. Can anyone guide me on how to resolve this issue?

The desired outcome is to see the changed value in the input field upon focus and blur events.

Answer №1

Instead of passing the value into the functions, you can use a Vue data property like shown below:

 const { ref, createApp } = Vue;
   
 const App = { 
    setup() {       
      const totalAmount = ref('')
      return {
        totalAmount
      }
   },
   methods: {
      hideSymbol() {
        this.totalAmount = this.totalAmount.slice(0, -2);
      },
      showSymbol () {
        this.totalAmount = `${this.totalAmount} s`
      }
   }
}

createApp(App).mount('#app')
<div id="app">
    <input
      v-model="totalAmount"
      @focus="hideSymbol()"
      @blur="showSymbol()"
    />
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"> 
</script>

If you decide to include an 's' in your value when submitting the form, ensure that is what you truly want.

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 Vue.js route is not aligning with its defined path, causing a mismatch

Attempting to develop a Vue SPA app, but encountering an issue with the routes not matching what was defined in the router. Despite all configurations seemingly correct, there is confusion as to why this discrepancy exists. What element might be overlooked ...

Using Angular, you can effortlessly inject elements into the editable div from any location on the page

Currently, I am working on developing an HTML interface that allows users to input text and send it as a notification to our mobile application. However, I am encountering challenges with the text and dynamically inserted elements using Angular 5; The te ...

Utilizing TypeScript to define React interfaces

How can I effectively utilize two interfaces for the same object? For instance: interface interfaceOne { id: string color: string } interface interfaceTwo { id: string numb: number } I have an Item component that is designed to receive an item ob ...

Accessing references within composable functions

Presented below is the code for my Vue3 application: <template> {{ names_data }} </template> <script> import getData from "./composables/getData" export default { name: "App", setup() { var filenames = [&qu ...

Mastering the use of v-if and v-else in Vue JS for <td> table elements

After exhausting my search efforts without finding a solution, I am turning to this platform for clarification on how to correctly implement the v-if and v-else directives within <td> elements in an HTML table in Vue. //assuming that the value of th ...

typescript: textual depiction of a generic type T

I have a requirement to develop a method that is capable of handling generic data types, and I need to incorporate the type information into the method Our API requires passing the entity name as a parameter: http://server/api/fetch/Entity/123 It's ...

Leverage a single attribute from a Typescript interface within another interface

Let's imagine we have a TypeScript Interface like this export interface IMyObj { id: string; type: 'AA' | 'AZ' | 'XY'; ... } Now, I require another interface that includes the same type field export interfa ...

I don't understand what's happening with this ternary format in the Typescript function - something seems off

Exploring Typescript. While browsing through a project's codebase, I stumbled upon the following snippet and am unsure of its validity. Can anyone shed light on what this code is doing? It seems to be dealing with default values, but I'm not enti ...

Encountering a "Module parse failed" error with type annotations in Nextjs while using Yarn Workspaces

I decided to experiment with transitioning a project from using Vite and React to Next.js and React. After reviewing the documentation on this page: https://nextjs.org/learn-pages-router/foundations/from-react-to-nextjs/getting-started-with-nextjs I made t ...

Are 'const' and 'let' interchangeable in Typescript?

Exploring AngularJS 2 and Typescript led me to create something using these technologies as a way to grasp the basics of Typescript. Through various sources, I delved into modules, Typescript concepts, with one particularly interesting topic discussing the ...

What are the steps for integrating and expanding a JavaScript library using rollup, TypeScript, and Angular 2?

I am currently working on a project called angular2-google-maps-test and I am interested in integrating and expanding upon the JS library found at js-marker-clusterer npm install --save js-marker-clusterer It seems that this library is not structured as ...

Can anyone explain why the Splice function is removing the element at index 1 instead of index 0 as I specified?

selectedItems= [5,47] if(this.selectedItems.length > 1) { this.selectedItems= this.selectedItems.splice(0,1); } I am attempting to remove the element at index 0 which is 5 but unexpectedly it deletes the element at index ...

Showing an error message upon submission in Angular 4 according to the server's response

Struggling for hours to display an error message when a form submits and returns an error status code. The solution seems elusive... In the login form component below, I've indicated where I would like to indicate whether the form is valid or invalid ...

Error: The property '...' is not found in the ReactElement<any, any> type, but it is required in the type '{...}'

As a beginner in TypeScript, I am currently working on rendering a page by fetching data from getStaticProps. The code snippet I am using for this purpose is: import React, {FormEvent, useState} from "react"; import { InferGetStaticPropsType } fr ...

Invoking a function on an object of a subclass that derives from an abstract class

In the realm of abstract classes, behold this one: export abstract class BaseStepComponent { /** Behold thy base-step ctor */ constructor() { } abstract getValue(): string; } And lo, here is a component that inherits such abstract glory ...

Creating a factory function in TypeScript to generate union types

I have developed a unique Factory type that allows me to create factory functions. export type Factory<T> = (state?: Partial<T>) => T; Within my <Avatar /> React component, I have implemented a prop with a union type to accommodate fo ...

Upon submission in Vue, the data variable becomes undefined

I set isError to false in the data, but when there is an error from Laravel, I receive a 422 error. I want to then set isError to true, but when I do, I get an error in the console saying that isError is undefined even though it has been defined. What coul ...

Troubleshooting Error: "Custom Vue component installation in Laravel 5 is not found in the npm registry"

We have decided to integrate Vue.js into our Laravel technology stack. In the app.js file, we have included the following Vue component: App.js file Vue.component('main-chart-of-accounts', require('chart_of_account.vue').default); Th ...

Angular displays [object Object] upon return

I have been struggling to send a post request, but unfortunately the API is returning undefined. When I try to send it to my API, it shows up as [object Object] getAccouting():Observable<any>{ // let json = this.http.get<any>('/assets/ ...

Is there a way to properly assign an index to a multidimensional array in a Vue.js template?

My setup involves utilizing Vue along with a multidimensional array structured like this: myArray = [[1,2,3],[1,2,3],[1,2,3]] To achieve the desired HTML layout shown below (ensuring that data-item counter increments correctly): <div class="row" data ...