I have a form (parent) component with two input text fields and date field for which vuejs-datepicker component is used (child component. See below:
Form component
<template>
<div>
<form>
Name<input type="text" v-model="name"/><br>
Age <input type="text" v-model="age"/><br>
<calendar2 @wybrano="zm($event)"></calendar2>
<button type="button" @click="sendfo()">Wyslij</button>
</form>
</div>
</template>
<script>
export default {
data() {
return {
name:"",
age:"",
da:""
}
},
created() {
},
methods: {
zm: function(ev) {
this.da=ev
},
sendfo: function() {
var t = this
axios({
method:"POST",
url: "sendfo",
data: {
name:"messi",
age:32,
data:t.da
},
}).then(function(response) {
console.log('done');
})
}
}
}
</script>
Calendar component
<template>
<div>
<datepicker v-model="data" name="data" :format="ff" :monday-
first="mondayFirst" :clear-button="clearButton" :calendar-
button="calendarButton" @selected="sel"></datepicker>
</div>
</template>
<script>
import Datepicker from 'vuejs-datepicker';
export default {
components: {
Datepicker
},
created() {
},
data() {
return {
data: "",
mondayFirst: true,
clearButton: true,
calendarButton: true,
}
},
methods: {
sel: function() {
if(this.data){
this.data=this.data.toISOString().substr(0,10)
this.$emit('wybrano', this.data)
}
},
}
}
</script>
My purpose is to use form component to collect form data and send them via ajax to database. As you can see in the code I use $emit
to transfer data from calendar component to the parent component. Everything work almost fine but... Selected (by user) date in calendar component is different than this date transferred to the parent component. I give few examples;
date selected in calendar 07/08/2018 -> var data="07/08/2018" -> var da=""
date selected in calendar 09/12/2018 -> var data="09/12/2018" -> var da="07/08/2018"
date selected in calendar 01/01/2018 -> var data="01/01/2018" -> var da="09/12/2018"
I hope my problem is understandable. Every time var da
in parent component takes value from previous selection. Please help me solve the problem.