Apexcharts.js: How to access this inside an event function in vue

Created on 28 Jul 2020  路  2Comments  路  Source: apexcharts/apexcharts.js

Using Vue apex charts, i need to access the vue "this" context inside the function. the chart events functions set the "this" keyword to the function scope and i cant figure out how to access the vue "this"

 events: {
            dataPointSelection(event, chartContext, config) {
                   // how to use this within an event function?
                   this.vueData = event
            }
          },

Most helpful comment

Use an arrow function

mounted() {
  this.$refs.chart.updateOptions({
    chart: {
      events: {
        dataPointSelection: (event, chartContext, config) => {
          // you can call Vue methods now as "this" will point to the Vue instance when you use ES6 arrow function
        }
      }
    }
  })
}

All 2 comments

I was able to workaround with this, does anyone have a better method?

  mounted() {
    const that = this
    this.$refs.chart.updateOptions({
      chart: {
        events: {
          dataPointSelection(event, chartContext, config) {
            that.selectedDate = event
          }
        }
      }
    })
  },

Use an arrow function

mounted() {
  this.$refs.chart.updateOptions({
    chart: {
      events: {
        dataPointSelection: (event, chartContext, config) => {
          // you can call Vue methods now as "this" will point to the Vue instance when you use ES6 arrow function
        }
      }
    }
  })
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

georgehardy picture georgehardy  路  3Comments

felixalguzman picture felixalguzman  路  3Comments

maasha picture maasha  路  3Comments

artfulrobot picture artfulrobot  路  3Comments

Sumon-miazi picture Sumon-miazi  路  3Comments