[Vuejs]-Reference prop from callback

2๐Ÿ‘

โœ…

If you want to use this in your data you have to use a normal function so that the this keyword could be correctly bound to the Vue instance ..


export default {
    props: {
        test: {
            type: String,
            required: true,
        },
    },
    data() {
        const vm = this
        return {
            chartOptions: {
                tooltips: {
                    callbacks: {
                        label: function(tooltipItem, data) {
                            console.log(vm.test);
                            return '';
                        }
                    }
                }
            }
        }
    }
}
๐Ÿ‘คHusam Ibrahim

1๐Ÿ‘

Try to use an arrow function and define chartOptions as a computed property :

computed:{
  chartOptions(){
    return {
      tooltips: {
         callbacks: {
        label: (tooltipItem, data)=> {
            console.log(this.test);
            return '';
        }
      }
    }
  }
}

}

Leave a comment