javascript - Passing default data to Vue form component -


here simple vue 2.0 form component. consists of number input , button, e.g.:

enter image description here

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 because v-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

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -