vue.jsvuejs2vuejs-datepicker

Can't get the value of Date Range Picker (Vuejs)


I am a newbie in VueJS. I want to get the value of the date range that is selected and console.log it when the user clicks the button. However, whenever I click the button, the value printing in console is null. Kindly Help.

This is the code:

App.vue

<template>
  <div id="app">
    <VueRangedatePicker v-model="datepicker"></VueRangedatePicker>
    <button class="button" @click="showdata()" value="Test">Normal</button>
  </div>
</template>

<script>
import VueRangedatePicker from "vue-rangedate-picker";
export default {
  name: "App",
  components: {
    VueRangedatePicker
  },
  data() {
    return {
      datepicker: null
    };
  },
  methods: {
    showdata() {
      console.log("DATE PICKER", this.datepicker);
    }
  }
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

Code can be accessed here.


Solution

  • For v-model to work, a component needs to have a prop called value and emit an input event.

    VueRangedatePicker doesn't have that prop or event. Instead, it emits a selected event when it updates. You can listen for it using @selected. For example:

    <VueRangedatePicker @selected="updateDatePicker"></VueRangedatePicker>
    
      methods: {
        showdata() {
          console.log("DATE PICKER", this.datepicker);
          console.log("start: " + this.datepicker.start);
          console.log("end: " + this.datepicker.end);
        },
        updateDatePicker(value) {
          console.log("updating datepicker value");
          this.datepicker = value;
        }
    

    See updated code here.