开发者问题收集

[Vue warn]: v-on 处理程序中出错:“TypeError: 无法读取未定义的属性

2022-03-15
6351

尝试将数据发布到集合(使用 Vue(2.6.11)、Vuetify(2.4.0)、Vuex(3.6.2)、Vue-router(3.5.1)、Axios)但出现此错误。还没能修复它,不确定为什么它不起作用。

添加项目表单(AddItem.vue):

                <v-form ref="form" v-model="valid">
                    <v-text-field name="title" v-model="form.title" label="title" required>
                    </v-text-field>

                    <v-text-field name="description" v-model="form.description" label="Item Description" required>
                    </v-text-field>

                    <v-select name="categoryID" v-model="form.category" :items="categories" item-text="name" item-value="_id" label="Category" required>
                    </v-select>

                    <v-select name="qualityID" v-model="form.quality" :items="qualities" item-text="name" item-value="_id" label="Quality" required>
                    </v-select>

                    <v-text-field name="price" v-model="form.price" label="Price" required>
                    </v-text-field>

                    <v-file-input name="photo" v-model="form.photo" multiple label="Item photo(s)">
                    </v-file-input>

                    <v-btn rounded text :disabled="!valid" @click="addItem()">
                                Add
                            </v-btn>

                    <v-btn @click="reset">
                        Reset Form
                    </v-btn>
                </v-form>

<script>
    import GoBack from '@/components/GoBack'

    export default {
        name: "addItem",
        data: () => ({
            form: {
                title: "",
                description: "",
                category: "",
                quality: "",
                price: ""
            },
            categories: [
                { _id: "620a6acaff3f5cebc8370121", name: 'Food' },
                { _id: "620a6ae3ff3f5cebc8370123", name: 'Clothes' },
                { _id: "620a6af1ff3f5cebc8370125", name: 'Furniture' },
                { _id: "620a6b04ff3f5cebc8370127", name: 'Electronics' },
                { _id: "620a7a0fded499a220f386d1", name: 'Tools' },
                { _id: "620a7ca7178dada11844dbad", name: 'Toys' }
            ],
        }),
        methods: {
            addItem() {
                if (this.$refs.form.validate()) {
                    this.$store.dispatch('addItem', this.form)
                }
            }
        }
    };
</script>

store.js:

import Vue from 'vue'
import Vuex from 'vuex'
import axios from '@/config'
import router from '@/router'

Vue.use(Vuex)

export default new Vuex.Store({
  
  actions: {
    addItem() {
      axios
        .post(`/items`, {
          title: this.form.title,
          description: this.form.description,
          categoryID: this.form.category,
          qualityID: this.form.quality,
          price: this.form.price
        })
        .then(response => {
          console.log(response.data)
        })
        .catch(error => {
          console.log(error)
          console.log(error.response.data.message)
          router.push('/items').catch(() => {});
        })
    }
  }
})

错误:

错误消息

错误中的“title”专门引用了“this.store.title”中的“title”(例如,如果我将其更改为“this.store.title1”,则错误将开始引用“title1”),我以为我看不出它有什么问题,也找不到任何解决方案。如果有人知道我该如何解决这个问题,我将不胜感激,如果我能提供更多信息,请告诉我,感谢您的时间。

1个回答

在您的商店中,您尝试使用未定义的 this.form。您必须使用参数而不是使用 this.form。您已经在此处将参数传递给您的操作 this.$store.dispatch('addItem', this.form) ,因此在这一侧没有什么可做的,但您没有使用它。

import Vue from 'vue'
import Vuex from 'vuex'
import axios from '@/config'
import router from '@/router'

Vue.use(Vuex)

export default new Vuex.Store({
  
  actions: {
    addItem({},form) {
      axios
        .post(`/items`, {
          title: form.title,
          description: form.description,
          categoryID: form.category,
          qualityID: form.quality,
          price: form.price
        })
        .then(response => {
          console.log(response.data)
        })
        .catch(error => {
          console.log(error)
          console.log(error.response.data.message)
          router.push('/items').catch(() => {});
        })
    }
  }
})
jkoch
2022-03-15