【发布时间】:2019-07-24 15:40:38
【问题描述】:
您好,我是 vue 和 vuex 的新手。
我在存储中发布了一些数据,需要根据用户选择修改它的嵌套数组属性。 我正在尝试在我的商店中使用“操作”,但是当我使用“this.$store.dispatch”时,我得到“未捕获的 TypeError:state.storyInfo.push 不是函数”。 如果我使用“this.$store.commit”直接提交“mutations”,我会得到未修改的原始状态。
这是我所在州的数据:
{
"id": 1,
"title": "Story title",
"episodes": [
{
"id": 1,
"title": "Episode 1"
},
{
"id": 2,
"title": "Episode 2"
},
{
"id": 3,
"title": "Episode 3"
}
]
}
例如,如果用户选择 id = 2 的剧集,我希望状态变为:
{
"id": 1,
"title": "Story title",
"episodes": [
{
"id": 2,
"title": "Episode 2"
}
]
}
我的 store.js 看起来像这样:
export const store = new Vuex.Store({
state: {
storyInfo: null
},
mutations: {
setStory (state, payload) {
state.storyInfo = payload
},
addInfoToStory (state, payload) {
const data = this.state.episodes
state.storyInfo.push(data)
}
},
actions: {
addToStory (infoToAdd, payload) {
infoToAdd.commit('addInfoToStory', payload)
}
},
getters: {
selectedStoryInfo: state => state.storyInfo
}
})
组件中的方法如下所示:
methods: {
addSelectedEpisode () {
this.$store.commit('addToStory', this.episode) // <- here if I use dispatch I get the error and no result
},
redirectTo () {
this.$router.push({name: 'Read'})
}
}
我也尝试修改网上找到的一些 TODO 示例,但我所做的似乎没有任何效果。
我做错了什么?
【问题讨论】: