【发布时间】:2020-02-01 08:16:46
【问题描述】:
我有一个由商店状态组装而成的计算数组:
computed: {
...mapGetters([
'$tg',
]),
...mapState({
podcastList: state => state.listening.podcastList,
}),
tabList: {
get() {
const questionTitle = this.$tg('questions');
const list = this.podcastList.map((poadcast, index) => ({
...poadcast,
title: `${questionTitle}${index + 1}`,
answers: [...poadcast.answers],
}));
return list;
},
set(value) {
// I want dispatch action here..
console.log('set', value);
},
},
}
podcastList的构造是一个对象数组:
[
{
id: 1,
answers: [
{ id: 1, content:'foo'}, { id: 2, content: 'bar'}
]
}, //.....
]
我正在使用v-for 来制作一组input,它与answers 绑定。
它看起来像:
<div class="columns is-vcentered" v-for="(answer, index) in tab.answers" :key="index">
<input type="text" v-model="answer.content"/>
</div>
// tab is an element of my tabList
我的问题:如果我更改了 input 的值,则不会触发计算的 setter。我会收到消息的
“错误:[vuex] 不在突变处理程序之外改变 vuex 存储状态。”
我知道我不能直接修改状态,但是我不知道如何分发动作,例如official website。有人可以帮忙吗?非常感谢。
【问题讨论】:
-
致电
set时要更改什么?您能否显示触发该错误的代码,它将帮助我们更好地了解您尝试执行的操作 -
我想更新“podcastList”的状态。当我更改输入值时弹出错误(在 vuex.esm.js 中)
-
我想保持 v-model 的灵活性,也要遵循 vuex 的模式。
标签: javascript vue.js vuex