new Vuex.Store({
state: {
comments: {
//store comments collection of each article
/* articleId: [...commentsCollection] */
},
articles: {
ids: [],
collection: []
}
},
mutations: {
//add a comment to comments collection of an article
ADD_COMMENT(state, comment){
state.comments[comment.articleId].push(comment);
//I'm not sure if below line is needed (it works without it tho)
Vue.set(state.comments, comment.articleId, state.comments[comment.articleId]);
}
}
});
- 我的第一个问题是如何在不使用Vue.set的情况下对对象进行变异(这里)?
第二,在我们更新
comments
特定文章的评论状态时,如果观察每篇文章中的评论并计算属性如comments(){ return this.$store.comments[this.articleId]; }
,那么其他文章的评论是否也会更新?如果是这种情况,那么执行不好吗?如何使用Vuex以非动态方式存储集合?最后是否有更好的方式来存储评论?
谢谢!
当你第二次尝试时发生了什么? –
@AmreshVenugopal按预期计算的工作 – user3211198