简体   繁体   English

"在 Nuxt 的 vuex 存储中更新动态对象中的值"

[英]Update value in dynamic object in vuex store in Nuxt

In my Vue/Nuxt project I have a form where the user can add update dynamic fields to be used in a price offer calculation.在我的 Vue/Nuxt 项目中,我有一个表单,用户可以在其中添加更新动态字段以用于报价计算。

When the form loads there will be created one field with the beforeMount lifecycle, and the user can then choose to create one or more extra fields.当表单加载时,将创建一个具有 beforeMount 生命周期的字段,然后用户可以选择创建一个或多个额外的字段。

in my data return I have this:在我的数据返回中,我有这个:

data() {
 return {
      calculationFields: { qty: 0, price: 3.86, selected: false },    
     }
 }

And when the user click the "Add field" button the addField method is called:当用户单击“添加字段”按钮时,会调用 addField 方法:

addField() {
      this.$store.dispatch('quantity/updateAdd', this.calculationFields)
    },

where the updateAdd actions calls the UPDATE_ADD_ITEM mutation updateAdd 操作调用 UPDATE_ADD_ITEM 突变的地方

UPDATE_ADD_ITEM(state, value) {
    state.options.push(value)
  },

value is { qty: 0, price: 3.86, selected: false } value { qty: 0, price: 3.86, selected: false }

this works fine as the options array is updated with the new field object这很好用,因为选项数组是用新的字段对象更新的

In the template I loop on this array to output X number of fields在模板中,我循环这个数组以输出 X 个字段

<div v-for="(field, index) in getCalculationFields()" :key="field" class="flex-xs justify-between calculation-field">
        <InputCustomPlaceholder type="number" :is-required="true" :input-id="`calculation-${index}`" :input-name="`calculation-${index}`" label-text="" placeholder-text="Add pieces" custom-placeholder-text="pcs" extraclass="flex-1 no-margin" />

        <a href="#" class="remove-field" @click.prevent="removeField(index)">&times;</a>
      </div>

My problem now is, that I can't figure out how I can use v-model on each dynamically created input fields so that I can update the qty value in the field object in the options state.我现在的问题是,我无法弄清楚如何在每个动态创建的输入字段上使用 v-model,以便我可以在选项状态下更新字段对象中的 qty 值。

So if the list contains three fields like:因此,如果列表包含三个字段,例如:

[
{ qty: 0, price: 3.86, selected: false }, 
{ qty: 0, price: 3.86, selected: false }, 
{ qty: 0, price: 3.86, selected: false }
]

So when the use in field number 2 input 200 as quantity the array will look like this:因此,当在字段号 2 中使用输入 200 作为数量时,数组将如下所示:

[
    { qty: 0, price: 3.86, selected: false }, 
    { qty: 200, price: 3.86, selected: false }, 
    { qty: 0, price: 3.86, selected: false }
]

I believe I have to use something like我相信我必须使用类似的东西

<InputCustomPlaceholder type="number" :is-required="true" :input-id="`calculation-${index}`" v-model="updateOptionList" :input-name="`calculation-${index}`" label-text="" placeholder-text="Add pieces" custom-placeholder-text="pcs" extraclass="flex-1 no-margin" />

But what is the best what to find the index of the field and update the value in that index on the array.但是最好的方法是找到字段的索引并更新数组上该索引中的值。

In a non-dynamic input I use something like this:在非动态输入中,我使用如下内容:

v-model="updateFieldOne"
updateFieldOne: {
    set(value) {
            this.$store.dispatch('fields/updatePartDimeWidth', value)
          }
}

which works as intended.按预期工作。

You can give index props, and use it in mutation.您可以提供index道具,并在突变中使用它。 EDIT_ITEM mutation help for edit item if you want add new elemenet use state.options.push(value) . EDIT_ITEM如果您想添加新的 elemenet 使用state.options.push(value)来帮助编辑项目。

this.$store.dispatch('fields/updatePartDimeWidth', {value ,index})

EDIT_ITEM(state, {value ,index }) {
    state.options[i] = value
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM