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

120
Visualizações
watch is not triggered when empty the ref object

I have created a dynamic model for filtering in vue 3 and have some problem when clear (empty the object)

 setup() {
    const model = ref({});

    watch(model.value, (value) => {
      console.log("model changed");
      console.log(model.value);
    });

    const addLanguage = (language) => {
      model.value["meta_" + language.toLowerCase()] = language;
    };

    const clear = () => {
      model.value = {};
    };

    return {
      addLanguage,
      clear,
    };
  },

I have also created a sample here for test https://codesandbox.io/s/mystifying-chebyshev-9tjogk?file=/src/App.vue

Adding new element to the model... the watcher is triggered .... but when you clear the object (empty) the watch function is no more triggered.

Any idea why ?

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

0

The watch source (i.e., model.value) gets replaced in clear() with an empty object, so the watcher no longer gets triggered.

Solution

  1. Instead of unwrapping the model ref, pass the ref itself as the watch source.

  2. Pass the deep flag to the watch so that property addition/deletion is observed.

watch(
  model, 1️⃣
  (value) => { /* handle change */ },
  { deep: true } 2️⃣
)

demo

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