我的组件:
<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')
}
1条答案
按热度按时间ddrv8njm1#
您得到该错误的原因是传递给
prepareEventForm
突变的event
对象是undefined
。这是因为当您调用
$store.dispatch('prepareEventForm', event)
时,它会调用您的prepareEventForm
action,并将event
作为第二个参数传入。您需要添加
event
作为action的第二个参数,并将其作为commit
调用中的第二个参数传递(这就是调用prepareEventForm
mutation 的原因):