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

132
Visualizações
Vue V-Model changes multiple Objects

When i use the mounted function in vue to assign two different objects in the data area and bind one of them to a form i get a weird problem: Both Objects changes with the input of values in the form

Example:

<template>
    <v-card>
        <v-form>

        <v-text-field
        v-model="newProduct.name"
        ></v-text-field>

        <v-text-field
        v-model="newProduct.price.net"
        ></v-text-field>

        </v-form>
    </v-card>
  
</template>

<script>
export default {
    data() {
        return {
            originalProduct: {}
            newProduct:{}
        }            
    },
    mounted () {
        const productFromApi = {
            name: 'test'
            price: {
                net:20
            }
        }
        this.originalProduct = productFromApi
        this.newProduct = productFromApi
    }
}
</script>

In this example the originalProduct changes also when form is edited

When I assign the objects with Object.assign just the inline object price changes with the binded object newProduct

I don't want, that the originalProduct is changed

Does anyone have a solution for this?

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

0

You need to make a deep copy of the object if you want to completely isolate the changes:

this.newProduct = JSON.parse(JSON.stringify(productFromApi))
about 4 years ago · Juan Pablo Isaza Relatório

0

When you the assignments, you're giving both originalProduct and newProduct a reference to the object productFromApi. So when you modify productFromApi, both will reflect the changes.

Try cloning the objects like this:

   this.originalProduct = {...productFromApi}
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