Why does vuejs say that this function does not exist? if it is working ok

Asked

Viewed 31 times

1

I have the following code below the function openDados(), it works more within the return context of the call socket does not work because..

I remember seeing something similar like this = this something kind of but I can’t remember where it was.... follow the code for the beasts to help...

mounted () {
   this.openDados() // aqui funciona funciona
   this.socket.emit('find-user', {
      userid: this.id
    }, function (data) {
      this.openDados() // está func que não funciona
      console.log(data)
      if (data.length <= 0) {
        console.log('entrou aqui') // teste e chega aqui
      } else {
        const payload2 = {
          id: data.userid,
          name: data.name,
          display: data.display,
          descript: data.descript,
          avatar: data.avatar
        }
        this.USER(payload2)
      }
    })
  },
  methods: {
    ...mapMutations('config', ['SET_DISPLAY_LEFT']),
    ...mapMutations('user', ['USER']),
    mostrarImagem (imagem) {
      if (imagem !== null) {
        return process.env.API_S3 + '/wbynet/' + imagem
      } else {
        return 'statics/boy-avatar.png'
      }
    },
    openDados () {
      this.openDadosConf = true
    },

the error I receive

inserir a descrição da imagem aqui

1 answer

2


This happens because this inside socket is another context. The method you call belongs to the external "this". So, before you call "this.socket...", create something like this:

const self = this;

So inside the socket you call:

self.openData();

And for everything else that refers to the external "this". Take the test and tell if solved. Success for Voce;!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.