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

200
Visualizações
Cannot watch a cloned value

Here is minimal example of my problem. I tried to find a similar question on SO but didn't found anything, so sorry if it's a duplicate.

App.vue

 <template>
  <div>
    <SubComponent v-model="myValue" />
    <SubComponent v-model="myClonedValue" />
  </div>
</template>

<script>
import SubComponent from "./components/SubComponent";

export default {
  components: { SubComponent },
  data: function () {
    return {
      myValue: {
        foo: 45,
        bar: 38,
      },
    };
  },
  computed: {
    myClonedValue: function () {
      return { ...this.myValue };
    },
  },
  watch: {
    "myValue.foo": function (val, oldVal) {
      console.log(val, oldVal);
    },
    /* This Doesn't work either

    "myClonedValue.foo": function (val, oldVal) {
      console.log(val, oldVal);
    },
    */
    myClonedValue: {
      handler: function (val, oldVal) {
        console.log(val, oldVal);
      },
      deep: true,
    },
  },
};
</script>

SubComponent

<template>
  <input v-model="value.foo" type="text" />
</template>

<script>
export default {
  name: "SubComponent",
  props: {
    msg: String,
    value: Object,
  },
};
</script>

Problem

In my main use case, I've a object that I want to modify only if the changes are confirmed. So I clone its values with computed property and for some reason I need to watch if some values have been modified. My problem here, as you can see it online on this CodeSandBox CodeSandBox project Is that VueJS doesn't detect any change once I clone the data value. If you change the second input of my example, nothing happens.

How can I solve this ?

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

0

Computed property doesn't just clone the original value once. It creates a reactive binding and recomputes every time the original value changes. So you need a different approach.

Make clonedValue a regular data property and assign a value to it in created or mounted lifecycle hook. Then you will be able to watch it just as your original value. And what (i assume) is more important, now you will be able to modify the original and cloned values independently.

data: {
    myValue: { foo: 1, bar: 2 },
    clonedValue: {}
},
created() {
    this.clonedValue = {...this.myValue}
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use deep watchers to achieve that, but you may have to check the inner keys yourself

watch: {
    myValue: {
        handler (val, oldVal) {
            if(val.prop !== oldVal.prop){
               // do something
            }
        },
        deep: true,
    },
},
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