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

230
Visualizações
Why can't I access to my data function from my computed property?

I have this in my data :

 data() {
    return {
      hello: "hello",
   }
}

And I have a computed property like this :

computed: {
    serviceHello() {
      return {
        sayHello() {
          console.log(this.hello);
        },
        sayHi(){
          console.log("hi");
        }
    }
}

When I'm calling my computed properties like this in my mounted.

this.serviceHello.sayHello(); // console : undefined
this.serviceHello.sayHi(); //console write "hi"

So I tried to see what is inside this but there's only the content from the computed(sayHello and sayHi), I can't access to my values in my data.

My question is, how can I access to the data from my computed ? I want the sayHello to display the hello from my data.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Your computed functions are wrong. It should be:

computed: {
    sayHello() {
      console.log(this.hello);
    },
    sayHi() {
      console.log("hi");
    }
}

The way you have wrote it means that the keyword this will be referencing to the object context itself and not to the context of the component.

about 4 years ago · Juan Pablo Isaza Relatório

0

You are using the wrong way a computed property. Computed properties must return a value;

Try this way.

computed: {
  sayHello() {
    return this.hello;
  },
  sayHi() {
    return "hi";
  }
}

and access as this.sayHello or {{sayHello}} in templates.

If you are trying to use a computed Setter you can use as this example below:

computed: {
  fullName: {
    // getter
    get: function () {
      return this.firstName + ' ' + this.lastName
    },
    // setter
    set: function (newValue) {
      var names = newValue.split(' ')
      this.firstName = names[0]
      this.lastName = names[names.length - 1]
    }
  }
}

More infor here https://v2.vuejs.org/v2/guide/computed.html

about 4 years ago · Juan Pablo Isaza Relatório

0

Just assign this to a global variable and use inside the returned function :

new Vue({
  el: "#app",
  data() {
    return {
      message: 'Welcome to Vue !',
    };
  },
  computed: {
    serviceHello() {
      let vm = this //assign it here to a global variable vm
      return {
        sayHello() {
          console.log(vm.message);//use vm here
        },
        sayHi() {
          console.log("hi");
        }
      }
    },
  },
  methods: {
    doSomething() {
      this.serviceHello.sayHello();
      this.serviceHello.sayHi();
    }
  },
  mounted(){
   this.serviceHello.sayHello();
      this.serviceHello.sayHi();
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">

  <h1>{{ message }}</h1>

  <button @click="doSomething">Say hello.</button>
</div>

This works fine but it's not the job of the computed property which should be used to return some stuff based on other properties

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