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

235
Visualizações
Vue : Why doesn't this `this` work normally?

case 1

data(){
  return {
    serverNames : ['local']
  }
}
computed: {
    ...mapState('store', {
      local_server_name: (state) => {
        this.serverNames[0] = state.local_server_name // Cannot read property 'serverNames' of undefined
        return state.local_server_name
      },
    }),
  },

case 2

computed: {
        ...mapState('store', {
          local_server_name: (state) => {
            serverNames[0] = state.local_server_name // serverNames is not defined
            return state.local_server_name
          },
        }),
      },

In fact, appending this or not, it doesn't work. If I want to use arrow function, then how to get that serverNames array?

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

0

The arrow function will preserve this based on the scope it was defined within, which is inside the object passed as a parameter to mapState where this refers to the global object (window).

But that's besides the point because you shouldn't be mutating state within a computed property; this can lead to infinite re-renders and/or Vue will complain.

If you want to update component state from Vuex state then you should do so within a watcher. Maybe something like this:

data() {
  return {
    serverNames: ['local']
  }
},

computed: {
  ...mapState('store', [
    'local_server_name'
  ])
},

watch: {
  local_server_name(value) {
    this.serverNames[0] = value
  }
}
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