Supplying preset information to Vue form element

Looking at this Vue 2.0 form component, you will find a number input and a button:

https://i.sstatic.net/SaruX.png

It's worth noting that the input value is connected to the data of the component through v-model, while the buttonText is passed as a prop.

Wondering how to set a default value for the form, so it doesn't start with 10?

  • Using props may not work well as it can interfere with v-model.
  • On the other hand, data seems unable to be passed similar to props according to Vue documentation.

.

<template>
    <form v-on:submit.prevent="onSubmit">
      <input v-model="amount" type="number" min="1" max="20"></input>
      <button type="submit">{{ buttonText }}</button>
    </form>
</template>

<script>
  export default {
    props: [ 'buttonText' ],
    data: function() {
      return {
        amount: 10
      }
    },
    methods: {
      onSubmit: function() {
        this.$emit("submit", parseInt(this.amount) );
      }
    }
  }
</script>

Answer №1

If you want to set an initial value for a prop (let's call it initialAmount), you can use that value when defining the initial value of amount in the data function:

export default {
  props: {
    buttonText: { type: String },
    initialAmount: { type: Number, default: 10 },
  },
  data: function() {
    return {
      amount: this.initialAmount
    }
  },
  methods: {
    onSubmit: function() {
      this.$emit("submit", parseInt(this.amount) );
    }
  }
}

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

Unexpected behavior with scrollTop

Note Reopening bounty as I forgot to award it last time. This question has already been answered by Master A.Woff. I am looking for a way to automatically scroll to a specific row when a user expands it, so that the content is immediately visible witho ...

Exploring the power of Angular JS promises through ng-repeat

My current project involves fetching latitude and longitude coordinates from a postcode using an API, then utilizing those coordinates to retrieve data on street level crimes near that location on a specific date through the UK police API. However, I have ...

The function you are trying to call is not callable. The type 'Promise<void>' does not have any call signatures. This issue is related to Mongodb and Nodejs

Currently, I am attempting to establish a connection between MongoDB and Node (ts). However, during the connection process, I encountered an error stating: "This expression is not callable. Type 'Promise<void>' has no call signatures" da ...

Tips for integrating search functionality with vanilla javascript?

My contact list app displays contact details and I am looking to add a search feature based on the contact's name. This functionality will allow the user to dynamically filter the contact list as they type in a specific name. Here is a snippet of the ...

What are the steps for importing and utilizing data from a JSON object?

After successfully importing data from a JSON file that was an Array, I've encountered a new challenge - my variable is now an object. My goal is to push all the questions from the object into an array and then display them on the HTML document. You c ...

Can the global mixin object in Vue.js be accessed outside of the Vue component?

I recently started working with Vue.js Currently, I am using version 2.4.4 of Vue.js in my project. In my app.js file, I have created a global mixin: ... import router from './router' ...(some imports and plugins definitions) Vue.use(VeeValida ...

The combination of Firebase and AngularJS with limitToFirst(1) fails to return a specific child element

Currently, I'm working with AngularJS and Firebase and facing an issue when trying to retrieve the first child from a filtered set of children. Interestingly, using limitToFirst(1) doesn't seem to accomplish this, unlike the .child("=KDhddgd47nd" ...

Changing Angular code to vanilla JavaScript

Here is an example code snippet for a plugin used in an Ionic/Capacitor/Angular project: import { ForegroundService } from '@awesome-cordova-plugins/foreground-service/ngx'; constructor(public foregroundService: ForegroundService) { } ... sta ...

prevent parent jQuery functions from interfering with child function execution

I am facing an issue with conflicting jQuery functions between parent and child elements. The child function is a bootstrap function, while the parent is a custom function. The main objective is to limit the height of the parent div (refer to the code belo ...

leveraging parcel for importing typescript dependencies

I am currently using parcel to process typescript for a web extension. I have installed JQuery and its type definitions via npm. In my typescript file, I have the following at the top: import $ from "jquery"; import "bootstrap"; However, when running run ...

Is there a way to trigger this floating menu to appear only when certain #divs are in view?

I was looking to create a floating menu and came across a helpful script on here /* Script by: www.jtricks.com * Version: 1.12 (20120823) * Latest version: www.jtricks.com/javascript/navigation/floating.html * * License: * GNU/GPL v2 or later http:// ...

Display a vibrant welcome screen on an Android WebView that ensures a visually appealing experience while the content loads for the

When launching an application, the desired behavior is as follows: Display a splash screen while simultaneously loading a URL Upon the firing of a JavaScript interface event on page load, remove the splash screen Mainactivity.java myWebView.addJavascript ...

How to automatically adjust select view in AngularJS as model value is updated

My select element is structured as follows: <select data-ng-model="transaction.category" class="form-control" data-ng-options="category.name group by category.parent for category in categories" required> </ ...

Positioning tooltip arrows in Highcharts

I'm attempting to modify the Highcharts tooltip for a stacked column chart in order to have the arrow on the tooltip point to the center of the bar. I understand that I can utilize the positioner callback to adjust the tooltip's position, but it ...

Oops! We encountered a TypeError where the property "match" cannot be read because it is undefined

An issue arises when running npm run build: ERROR TypeError: Cannot read property 'match' of undefined. - Building for production... ERROR TypeError: Cannot read property 'match' of undefined TypeError: Cannot read property &apo ...

What are the best techniques for resizing a bar graph column to perfectly and proportionally match its container size?

Creating a basic bar chart library using vanilla JS and jQuery is what I'm working on. The code below is functional: var userData = { 'Black': 8, 'Latino': 6, 'Native': 4, 'Asian': 10, 'White': 12, & ...

Looking for ways to detect memory leaks in your JavaScript application using Selenium?

While utilizing Java and Selenium for automated testing of a JavaScript web application, the issue of memory leaks has arisen. I am interested in ways to effectively test for them. Is there a simple method to obtain memory usage and other profiling data fo ...

Capture keyboard input using socket.io

My goal is to capture individual keystroke events (keydown and keyup) in a chat client. The current code I have sets up a chat application that sends each message to a mongoDB cluster. However, I want to create a new record for each keystroke event rather ...

Editing a Text File using JavaScript

Looking for assistance in JavaScript to write data into a text file. The goal is to input a username and password, with each entry creating a new line in the text file. Check out my code snippet here http://pastebin.com/24Tvdemu. I've been struggling ...

Trouble executing multiple get requests using request-json and async functions in node.js

I am currently working on a project that involves loading JSON data from multiple REST resources. To achieve this, I have set up a series of get requests that need to be completed before performing a specific function. Here is a snippet of the code I am u ...