Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

310
Visualizações
How to convert this arrow function to a normal function?

This is from a child component in Vue.js. I'm trying to pass some data from the parent in sensorData, but the binding isn't happening because the code below uses an arrow function for data. How can I convert this function to a normal function so that the this binding is available.

export default {
  name: "SensorChart",
  props: ["sensorData"],

  data: () => ({
    chartOptionsLine: {
      xAxis: {
        data: ["A","B","C","D","E","F","G"]
      },
      yAxis: {
        type: "value"
      },

      series: [
        {
          type: "line",
          // data: this.sensorData
          data: [910, 423, 61, 752, 262, 3625, 119]
        }
      ],
      title: {
        text: "Sample Data",
        x: "center",
        textStyle: {
          fontSize: 20
        }
      },
      color: ["#1271c2"]
    }
  })
};
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

This worked fine.

export default {
  name: "SensorChart",
  props: ["sampleData","sampleLabels"],

  data: function() { 
    return {
      chartOptionsLine: { 
        xAxis: {
          data: this.sampleLabels
        },
        yAxis: {
          type: "value"
        },

        series: [
          {
            type: "line",
            data: this.sampleData
          }
        ],
        title: {
          text: "Sensor Data",
          x: "center",
          textStyle: {
            fontSize: 24
          }
        },
        color: ["#127ac2"]
      }
    }
  }
};
about 4 years ago · Juan Pablo Isaza Relatório

0

An arrow function expression is a compact alternative to a traditional function expression, but is limited and can't be used in all situations.

There are differences between arrow functions and traditional functions, as well as some limitations:

  • Arrow functions don't have their own bindings to this, arguments or super, and should not be used as methods.
  • Arrow functions don't have access to the new.target keyword.
  • Arrow functions aren't suitable for call, apply and bind methods, which generally rely on establishing a scope.
  • Arrow functions cannot be used as constructors.
  • Arrow functions cannot use yield, within its body.

for more Details in there:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#comparing_traditional_functions_to_arrow_functions

So,if you want to use traditional way, you can use this.

export default {
  ...
  data: function() { return {...} }
}

if you want to use arrow function, While arrow functions do not have their own this pointer, only arguments can be passed when a function is called through call() or apply() (this way can't bind this).

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda