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