Search code examples
javascriptvue.jsflatpickr

How to access component data from flatpickr event method?


I'm currently using the vue-flatpickr-component to create much nicer date input fields. Now I have an "onClose()" method (see code below) to check if the entered date is valid.

I removed much of the code to make it more readable.

If it's not valid it should change a boolean value to true, but I can't access the value in the data from the onClose method. Does anyone know how I can do that? Thanks

export default {
  components: {
    flatPickr
  },
  data () {
    return {
      invalidDate: false,
      datepickerConfig: {
        wrap: true,
        altInput: true,
        allowInput: true,
        onClose () {
          // Set invalidDate to true
        }
      }
    }
  }
}

Solution

  • To access your data object from inside the onClose() function, you have to convert it to a arrow function, because the context of this changed. Understanding binding and 'this'

    ...
    onClose: () => {
       this.invalidDate = true;
    }
    ...