简体   繁体   English

使用 state 绑定表单时在 Nuxt JS 中出现突变错误

[英]Getting mutation error in Nuxt JS while binding form with state

I am trying to bind my form's fields with my vuex store in Nuxt JS.我正在尝试将表单的字段与 Nuxt JS 中的 vuex 存储绑定。 It works fine with normal text fields with get() set() in computed .它适用于带有get() set() in computed的普通文本字段。 But having trouble in customizing getting and setting manually.但是在手动自定义获取和设置时遇到了麻烦。 I am trying to push objects to an array in a specific format from my template to store and also keep the binding among them.我正在尝试将对象从我的模板中以特定格式推送到数组中以存储并保持它们之间的绑定。 Here is my code:这是我的代码:

<template>
  <div class="container setting-form-area-business">
    <b-form-group v-for="(input, index) in phoneNumbers" :key="`phoneInput-${index}`">
      <label>Mobile Number {{index+1}}*</label>
      <b-input-group>
        <b-form-input v-model="input.phone" @input="updateStore" class="custom-form-input-business">
        </b-form-input>
        <b-input-group-append v-show="phoneNumbers.length > 1">
          <b-button class="mobile-number-remove-btn" @click="removeField(index, phoneNumbers)"></b-button>
        </b-input-group-append>

      </b-input-group>

    </b-form-group>

    <b-form-group>

      <b-button class="jh-btn2" @click="addField">Add More Mobile Number</b-button>

    </b-form-group>

  </div>
</template>
<script>
  export default {
    props: [
      'visited'
    ],

    data() {
      return {
        phoneNumbers: this.$store.state.business.formvalue.mobileNumber.length ? this.$store.state.business.formvalue
          .mobileNumber : [{
            phone: ""
          }],


      }
    },
    computed: {
      mobilenumbers() {
        return this.$store.state.business.formvalue.mobileNumber
      },

    },
    methods: {

      addField() {
        this.phoneNumbers.push({
          value: ""
        });

      },
      removeField(index, fieldType) {

        fieldType.splice(index, 1);
        console.log('fieldType', fieldType);
        this.emitErrorStatus();
      },
      updateStore() {
        this.$store.commit('business/setformmobileNumber', {
          mobileNumber: this.phoneNumbers
        })
      }
    },
  }

</script>

this was working fine when I was in vue, but coming to nuxt, it is giving me error当我在 vue 时,这工作正常,但是来到 nuxt,它给了我错误

[vuex] do not mutate vuex store state outside mutation handlers. [vuex] 不要在突变处理程序之外改变 vuex 存储 state。

As told by the error, you should not mutate the state.如错误所述,您不应该改变 state。 There are several ways to handle this one.有几种方法可以处理这一问题。 A quick search here could give you a lot of answers.在这里快速搜索可以给你很多答案。

This is mine (using Lodash's cloneDeep ): https://stackoverflow.com/a/66262659/8816585这是我的(使用 Lodash 的cloneDeep ): https://stackoverflow.com/a/66262659/8816585

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

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