javascript - Passing default data to Vue form component -
here simple vue 2.0 form component. consists of number input , button, e.g.:
note value of input tied component's data
using v-model
. buttontext
passed in prop.
what's best way pass default value form, renders value other 10?
- using
props
doesn't seem right way becausev-model
no longer works properly. - however,
data
can't passed in way props can, far can tell 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>
you can pass in prop (say initialamount
) , reference when initializing amount
value in 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) ); } } }
Comments
Post a Comment