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

531
Visualizações
Why my Vue.js app isn't auto updating when I push and/or splice an object from the array?

I'm using Quasar and Vue.js to create a form where I can add objects into an array so they can be inserted into the DB all at the same time, but whenever I add or delete one object from this array it just updates after I focus on any other input in the form, it is adding and deleting the objects from the array as it should, but it isn't showing on the screen unless I focus on any other input in the form, follow the code:

<template>
  <form>
    <q-input
      v-model = "eventTitle"
      label = "Event title"
    />
    <div
      v-for = "(task, index) in tasks" :key = "index"
    >
      <q-btn
        label = "Remove Task"
        @click = "removeTask(index)"
      />
      <q-input
        v-model = "task.title"
        label = "Title"
      />
      <q-input
        v-model = "task.date"
        label = "Date"
      />
      <q-input
        v-model = "task.desc"
        label = "Description"
      />
    </div>

    <q-btn
      label = "Add Task"
      @click = "addTask"
    />
  </form>
</template>

<script>
  export default {
    setup () {

      const singleTask = {
       title: '',
       date: '',
       desc: ''
      }

      var tasks = []

      return {
        tasks,
        singleTask
      }
    },

    methods: {
      addTask() {
        this.tasks.push(this.singleTask)
      },

      removeTask(index) {
        this.tasks.splice(index, 1)
      }
    }
  }
</script>

And whenever I edit a field on one object the others are also modified (which is another problem). If someone has any suggestions on this one I'll be grateful because I've never seen something like this.

Thanks for the suggestions and answers beforehand.

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

0

Multiple problems here:

  1. singleTask is a single object instance. Pushing it into array will not create a copy of the object. As a result your array contains multiple items - references to an object all pointing to a single object. Google for "JS value vs reference" to understand the concept

  2. You are using setup but data structures returned from it are not reactive data. You need to use ref or reactive

  3. Do not use index as the key - it same as not using key at all. key must be stable - meaning it should "connect" any single object in the iterated array to a DOM element (or group of elements) created for it by Vue. Index is not stable as it changes when you delete items from the array

Last piece of advice: combining Composition API and Options API is not recommended. Either use one or another. Excellent new tutorial can help

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