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

170
Visualizações
can't hit vue method from within axios response

I can't figure out what's wrong here, but my axios call works properly and my data response comes back as I expect with success.

The issue now is when I try to call another vue method within my response success from axios. If I comment out the method call then it's fine, but if I leave it the console just reads undefined and I don't get the console.log from within my testFunction so it seems like I'm not even hitting it.

What have I done wrong?

var vm = 
new Vue({
  el: "#app",

  methods: {
   axiosFunc() {
    axios({
      method: 'post',
      url: test,
      data: {
        date:'2022-03-02',
      }

    }).then(function (response) {
      console.log('success');
      this.testFunction();

    }).catch(function (error) {
     
    });
   },
   testFunction() {
     console.log('in new function');
   }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<button @click="axiosFunc()">Console log</button>
</div>

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

0

Replace your

...
.then(function (response) {
  console.log('success');
  this.testFunction();
})
...

by

...
.then(response => {
  console.log(response);
  this.testFunction();
})
...

To avoid context override.

You can also use this code:

...
.then(function (response) {
  console.log(response);
  this.testFunction();
}.bind(this))
...

Or

...
const that = this;
...
.this(function (response) {
  console.log(response);
  that.testFunction();
})
...
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