【问题标题】:post indexed array Vue.js Axiom发布索引数组 Vue.js Axiom
【发布时间】:2020-08-19 17:19:23
【问题描述】:

我可以在 Vue 中发布一个对象数组,但是我无法发布一个简单的索引数组。我给出了简化的代码,因为我认为答案对于有经验的人来说是显而易见的。

这是我到目前为止所得到的......

<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>

目前为止的 Vue 实例数据对象:

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

什么有效:

  • 用户可以在表单上添加/删除行。
  • Vue DevTools 向我显示了一个索引数组,其中每一行都有空字段。

JS 控制台中的错误消息:

我想在帖子中看到这个...

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

相反,我只能得到这个,因为 Vue 不会对输入更改做出反应......

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

为了比较,发布一个对象数组的工作方式如下:

<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>

Vue 实例数据对象:

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

【问题讨论】:

  • 把 v-model="item[index]" 改成 v-model="item"

标签: vue.js axiom


【解决方案1】:

我刚刚在另一个论坛上收到了答案。 太傻了... item[index] 只需要是 items[index]。

【讨论】:

    猜你喜欢
    • 2011-08-29
    • 2021-08-03
    • 2017-12-31
    • 1970-01-01
    • 2016-06-28
    • 2020-02-22
    • 2021-08-31
    • 1970-01-01
    • 2017-10-19
    相关资源
    最近更新 更多