我的组件:
<div id="event-picker">
<template v-for="event in $store.state.events">
<a href="#" v-on:click.prevent="$store.dispatch('prepareEventForm', event)">{{ event.artist }}</a>
</template>
</div>
我的商店(突变):
prepareEventForm(state, event) {
state.form.time = event.time
state.form.date = event.date
state.form.event = event.event
state.form.artist = event.artist
state.form.organizer = event.organizer
state.form.location = event.location
state.showForm = true
}
我得到的错误是Cannot read property 'time' of undefined
哪里可能是问题?
编辑:
这是我的行动方法:
prepareEventForm({ commit }) {
commit('prepareEventForm')
}
答案 0 :(得分:1)
您收到该错误的原因是传递给event
突变的prepareEventForm
对象是undefined
。
这是因为当您致电$store.dispatch('prepareEventForm', event)
时,它会调用您的prepareEventForm
操作,并将event
作为第二个参数传递。
您需要添加event
作为操作的第二个参数,并将其作为commit
调用中的第二个参数传递(调用prepareEventForm
突变< / em>的):
prepareEventForm({ commit }, event) {
commit('prepareEventForm', event)
}