[Vue warn]: Error in v-on handler: "TypeError: Cannot read properties of undefined
[Vue warn]: Error in v-on handler: "TypeError: Cannot read properties of undefined
尝试 post 数据到集合(使用 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'), 以为我看不出有什么问题,也找不到任何解决方案。如果有人知道我该如何解决这个问题,我将不胜感激,如果我能提供更多信息,请告诉我,感谢您的宝贵时间。
在您的商店中,您正在尝试使用未定义的 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(() => {});
})
}
}
})
尝试 post 数据到集合(使用 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'), 以为我看不出有什么问题,也找不到任何解决方案。如果有人知道我该如何解决这个问题,我将不胜感激,如果我能提供更多信息,请告诉我,感谢您的宝贵时间。
在您的商店中,您正在尝试使用未定义的 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(() => {});
})
}
}
})