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

147
Visualizações
Changes to object after it is pushed to Vuex store reflect in the store

To create a new record, I define the record object within the components data() function, and bind it to form fields using v-model. This object is then passed to a Vuex action that POSTs the new record to the back end, and then sends the object to the corresponding mutator.

The issue is, when changes are made to the original object defined in data(), those appear to cascade to the object that was pushed into the store.

I understand the pass by reference concept, but it seems this pattern should not be affected. Is there a better pattern to use when creating a new DB record?

Add Record Vue

<v-text-field
    v-model="newRecord.name"
/>
...
export default {
    data() {
        return {
            newRecord: {}
        }
    }
}
...
methods: {
    addRecord(){
        this.addRecordAction(this.newRecord)
        ...
        this.newRecord.name = 'new name' // <== reflects in vuex store!
    }
}

Vuex Record Store

async addRecordAction({commit}, newRecord){
    const response = await axios.post('/api/record', newRecord)
    newRecord = response.data // contains record's new ID
    commit('addRecordMutation', newRecord)
}
...
addRecordMutation: (state, newRecord) => (state.records.push(newRecord))
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Because your addRecordAction is asynchronous, this.newRecord.name = 'new name' is likely being executed prior to the addRecordAction, and thus because you passed by reference the object being added has the new name.

Easiest solution 'await' addRecordAction before setting the name.

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