我在表单内部有一个子组件,该组件将对象作为父组件的道具。该对象用于存储通过输入从用户输入的数据,并在用户键入时发出。现在,我会说实话,我还不确定100%的发射功能还可以。这是由于以下事实:当我将对象的属性附加到输入元素内的v-model时,Nuxt会显示错误信息
Cannot read property 'insert property here' of undefined
令我感到困惑的是,如果我查看Vue开发工具的内部,就会清楚地表明该道具已经收到。单击子组件时,我可以查看其所有属性,但是当我尝试引用该子组件时,出现错误。
这是我如何将数据从父文件传递到子组件的方法。
<NewProjectIterations
v-for="i in itterations"
:key="i"
:index="i"
v-model="itterationsData[i]"
/>
这是JavaScript
export default {
name: 'NewProject',
components: {
NewProjectIterations
},
data() {
return {
itterations: 2,
payoutAmount: null,
projectName: null,
emailAssigned: null,
itterationsData: []
}
},
computed: {
// Computed property that keeps all the form data inside of one object
formData() {
return {
user: this.$store.auth.user._id,
projectName: this.projectName,
payoutAmount: this.payoutAmount,
payoutDividedInto: this.itterations,
emailAssigned: this.emailAssigned,
itteration: this.itterationsData
}
}
},
mounted() {
// Initialize the child components data object on mount inorder to store user submit data
const object = {
name: '',
title: '',
startDate: null,
endDate: null,
payoutAmount: this.payoutAmount / this.itterations,
filesAttachhed: null,
index: null
}
for (let i = 0; i < this.itterations; i++) {
object.index = i + 1
this.itterationsData.push(object)
}
},
methods: {
increaseItteration() {
if (this.itterations > 5) {
alert('Itterations cannot be more than 5, nice try')
return
}
const object = {
name: '',
title: '',
startDate: null,
endDate: null,
payoutAmount: this.payoutAmount / this.itterations,
filesAttachhed: null,
index: this.itterations + 1
}
this.itterationsData.push(object)
this.itterations = this.itterations++
},
removeItteration() {
if (this.itterations > 1) {
alert('Itterations must be more than 1, nice try')
return
}
this.itterations = this.itterations--
this.itterationsData.pop()
},
removeItterationByIndex(index) {
this.itterations = this.itterations--
this.itterationsData.splice(index)
this.itterationsData.forEach(({ index }, i) => {
index = i + 1
})
},
async submitProject() {
// Submit the data and make a request to the backends API through Vuex & store the data
// inside of a database
await this.$store.dispatch('projects/createNewProject', this.formData)
}
}
最后是<NewProjectItteration />
单个文件组件。
<template>
<div :id="index" class="New-Project-Iteration">
<div class="New-Iteration-Upper-Half">
<div class="New-Iteration-Index">
<p>{{ index + 1 }}</p>
</div>
<div class="New-Interation-Header">
<input
id="title"
v-model="value.name"
type="text"
placeholder="Enter Title"
name="title"
style="display: block"
/>
<input
id="date"
v-model="value.startDate"
placeholder="Start"
class="textbox-n"
type="text"
onfocus="(this.type='date')"
onblur="(this.type='text')"
/>
-
<input
id="date"
v-model="value.endDate"
placeholder="Deadline"
class="textbox-n"
type="text"
onfocus="(this.type='date')"
onblur="(this.type='text')"
/>
</div>
</div>
<div class="New-Iteration-Lower-Half">
<p class="New-Iteration-Files">
No Files Attached
</p>
<p class="New-Iteration-Money">
$<input
id="money"
v-model="value.payoutAmount"
type="number"
placeholder="00.00"
step="0.01"
min="0"
name="money"
/>
</p>
</div>
</div>
</template>
及其JavaScript
export default {
name: 'NewProjectIteration',
props: ['index', 'value'],
watch: {
value() {
this.$emit('input', this.value)
}
}
}
我的一部分感觉与Vue的生活方式挂钩有关,我使用不当。无论如何,我都想问这个问题,以防其他人遇到与我相同的问题,并希望阅读详细的答复。