【问题标题】:Vue dynamic v-model generating extra array item with null valueVue 动态 v-model 生成具有空值的额外数组项
【发布时间】:2021-07-31 09:22:37
【问题描述】:

我正在使用惯性 laravel 来更新来自 Vue 内联表输入的数据,

这是喷射流输入字段组件。

<jet-input type="text" v-model="inlineEdit.partner[item.id]"/>

item.id 是我的数据库增量 ID。

   data() {
        return {
            inlineEdit: this.$inertia.form({
                partner: [],
            }),
        }
    },
    methods: {
        updateInline() {
            this.inlineEdit.put(route('codes.update'))
        }
    }

这是我尝试更新时的结果。

array:6 [▼
  0 => null
  1 => null
  2 => null
  3 => null
  4 => null
  5 => "123"
]

5 是实际的 item.id 但不知道为什么它显示为 0 到 4。

【问题讨论】:

    标签: vue.js vuejs2 inertiajs jetstream


    【解决方案1】:

    如果你在数组中设置了第五个元素,那么它会自动扩展到所需的大小,并且在缺少的位置上包含空元素。

    可能你想使用关联数组(js 对象,{})而不是数组([]

    为此只需将partner 的初始值替换为空对象

                inlineEdit: this.$inertia.form({
                    partner: {}, // <-- there
                }),
    

    【讨论】:

    • 但是我怎么能在这里使用对象呢? item.id 具有动态值。
    • 更新了我的答案。您仍然可以使用方括号通过对象中的字符串键访问值
    • 也许您需要将 int 索引转换为字符串。例如&lt;jet-input type="text" v-model="inlineEdit.partner[item.id+""]"/&gt;
    猜你喜欢
    • 2019-09-15
    • 1970-01-01
    • 2018-03-14
    • 2020-10-29
    • 2021-11-27
    • 2022-08-13
    • 2018-11-03
    • 2019-02-14
    • 2021-12-18
    相关资源
    最近更新 更多