Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda