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

206
Visualizações
Field value is getting pushed to wrong array

In a vue app form, I have two text inputs and a file input.

What I want to happen: The text inputs should go into a fieldHeader array, and the file input should go into the fieldsArray array.

What does happen: Both text fields and the file input go into both the fieldHeader and the fieldsArray. The console.log for both arrays after clicking "add field" is

0: {field: 0, name: 'text', desc: 'text', imgOne: File}

It should be fieldHeader:

0: {field: 0, name: 'text', desc: 'text'}

and fieldsArray:

0: {imgOne: File}

I would think the v-model would push the items into the correct arrays, but that's not it. The think I am working through now is to restructure the v-for as that's the only place where I can currently see the code as mixing the two array concepts.

<template>
  <div v-for="(field, i) in fieldsArray"  :key="i" >
    <q-input
      label="Name"
      v-model="fieldHeader[i].name"
      type="text"
      dense
    />
    <q-input
      v-model="fieldHeader[i].desc"
      type="textarea"
    />
    <q-file
      v-model="fieldsArray[i].imgOne"
      @update:model-value="onSelected()"
    ></q-file>
  </div>
  <div @click="handleAddField">Add Field</div>
</template>
<script>
const handleAddFields = () => {
  let i = fieldsArray.value.length;

  const item = {
    field: i,
  };
  fieldHeader.value.push(item);
  fieldsArray.value.push(item);
  
};
</script>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try like following snippet:

const { ref } = Vue
const app = Vue.createApp({
  setup () {
    const fieldsArray = ref([])
    const fieldHeader = ref([])
    const handleAddField = () => {
      fieldHeader.value.push({name: '', desc: ''});
      fieldsArray.value.push({imgOne: null});
    };
    const onSelected = () => { }
    return { fieldsArray, fieldHeader, handleAddField, onSelected }
  }
})
app.use(Quasar)
app.mount('#q-app')
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons" rel="stylesheet" type="text/css">
<link href="https://cdn.jsdelivr.net/npm/quasar@2.4.12/dist/quasar.prod.css" rel="stylesheet" type="text/css">
<div id="q-app">
  <div v-for="(field, i) in fieldsArray" :key="i" >
    <q-input
       label="Name"
       v-model="fieldHeader[i].name"
       type="text"
       dense
     ></q-input>
     <q-input
       v-model="fieldHeader[i].desc"
       type="textarea"
     ></q-input>
     <q-file
       v-model="fieldsArray[i].imgOne"
       @update:model-value="onSelected()"
     ></q-file>
 </div>
 <q-btn @click="handleAddField">Add Field</q-btn>
 <p>fieldsArray: {{fieldsArray}}</p>
 <p>fieldHeader: {{fieldHeader}}</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.prod.js"></script>
<script src="https://cdn.jsdelivr.net/npm/quasar@2.4.12/dist/quasar.umd.prod.js"></script>

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