Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mvnobrega's avatar

How to access data inside a function in data()

I summarize my code as follows:

data () {
          return {
            currencyChart: 'USD',
            chartOptions: {
 
		        tooltip: {
				    formatter: function() { // Add the formatter function**
				      const date = new Date(this.x);
				      const preco = Number(this.y).toLocaleString('pt-BR', { style: 'currency', currency: this.currencyChart });
				      return date.toLocaleDateString() + '<br> <span style="margin-top: 10px;">&nbsp' +preco +'</span>';
				    }
				  },
		     

		      }
          }
        },

I need to get the currencyChart, but if I put this.currencyChart, because I am using a function it refers to the content of the function.

If I change the function to arrow: formatter: () => {} , then it works, but this.x and this.y are no longer part of the chart values.

I've never come across this situation and I don't know how to resolve it or the best way. I appreciate any help.

0 likes
7 replies
MaverickChan's avatar

Vue 2? you cannot define data like this. just add a computed property or a method to process the data.

Strongly suggest you use Vue 3.

gych's avatar

@mvnobrega Have you tried to use Composition API with script setup ? Much better dev experience when using Vue with that now.

mvnobrega's avatar

@gych It looks interesting, I'll study it a little more. Thanks for the tip

gych's avatar

@mvnobrega No problem :) Take your time, if you've more questions don't hesitate to reach out.

1 like

Please or to participate in this conversation.