3👍
✅
One of the easiest ways would be to emit the default value when the mounted
function fires.
mounted () {
this.$emit('selected', config.state.fromDate)
}
Other than that I would mutate the value into a prop and alter it slightly to allow it to work with v-model
so that the default value is passed from the parent using whichever method it chooses to derive that value.
EDIT:
Here’s how I would refactor it to use a prop.
<template>
<div>
<label class="col-form-label">
<span>From</span>
</label>
<DatepickerFrom v-on:selected="SetSelectedDateFrom" :value="value" :disabled="config.disabledFrom"></DatepickerFrom>
</div>
</template>
<script>
import DatepickerFrom from 'vuejs-datepicker'
const dUse = new Date()
export default {
props: {
value: {
type: String,
required: true
}
},
data() {
return {
config: {
disabledFrom: {
to: new Date(dUse.getFullYear() - 1, dUse.getMonth(), dUse.getDate()),
from: new Date(dUse.getFullYear(), dUse.getMonth(), dUse.getDate())
},
state: {
}
}
}
},
methods: {
SetSelectedDateFrom: function(selectedDate) {
this.$emit('input', selectedDate)
}
},
components: {
DatepickerFrom
}
}
</script>
now you can use your component with the v-model directive for 2 way binding
<component v-model="dateFrom"></component>
...
data () {
return {
dateFrom: new Date(
dUse.getFullYear(),
dUse.getMonth(),
dUse.getDate() - 1
).toString()
}
}
Source:stackexchange.com