0

I can post an array of objects in Vue, but I'm having trouble posting a simple indexed array. I'm giving streamlined code because I think the answer lies in plain sight for the experienced.

Here's what I've got so far...

<section v-for="(item, index) in items">
  <div>
    <button @click.prevent="deleteItem(index)">delete</button>
    <input type="text" v-model="item[index]" placeholder="enter your item here">
  </div>
</section>
<div>
  <button @click.prevent="addItem">add item</button>
</div>

The Vue instance data object so far:

data () {
  return {
    items: ['']
  }
},

What works:

  • The user can add/delete rows on the form.
  • Vue DevTools shows me an indexed array with empty fields for each row.

Error messages in the JS console: Vue error messages

I would like to see this in the post...

{ "items": ["item1 input value", "item2 input value"] }

Instead I'm only able to get this because Vue won't react to the input changes...

{ "items": ["", ""] }

For comparison, posting an array of objects works like this:

<section v-for="(item, index) in items">
  <div>
    <button @click.prevent="deleteItem(index)">delete</button>
    <input type="text" v-model="item.color" placeholder="enter color here">
    <input type="text" v-model="item.price" placeholder="enter price here">
    <input type="text" v-model="item.comment" placeholder="enter comment here">
  </div>
</section>
<div>
  <button @click.prevent="addItem">add item</button>
</div>

The Vue instance data object:

data () {
  return {
    items: [{ color: '', price: '', comment: '' }]
  }
},

1
  • Correct v-model="item[index]" to v-model="item" Commented May 5, 2020 at 8:49

1 Answer 1

0

I just received the answer in another forum. It was so silly... item[index] just needs to be items[index].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.