【发布时间】:2021-06-02 22:50:09
【问题描述】:
有一个子组件列表
<question-list-item
v-for="(item, index) in questionListParsed"
:key="item.id"
:both-question="item"
:class-id="classId"
:subject-id="subjectId"
:index="index+1"
/>
questionListParsed 是 vuex 中的 getter。
/**************************************************************************
* getters
**************************************************************************/
get questionListParsed(): QuestionListItemRes[] {
const { questionList, showingOriginalQuestion } = this
const questionListParsed = questionList.map((e) => {
const recommendQuestion = e.recommendedQuestions[0]
const recommendQuestionIds = showingOriginalQuestion[e.questionNumber]
let arr = []
if (recommendQuestionIds) {
arr = recommendQuestionIds.filter((item) => {
return !this.removedRecommendQuestionIds.includes(item)
})
}
return {
recommendQuestion: {
...recommendQuestion,
stem: recommendQuestion.question,
knowledges: splitMultiKnowledge(recommendQuestion.knowledge),
questionSourceList: recommendQuestion.sources,
categoryId: recommendQuestion.categoryId,
},
originalQuestion: {
...e,
id: e.questionNumber,
stem: e.question,
difficulty: e.complexity,
knowledges: splitMultiKnowledge(e.knowledge),
},
id: recommendQuestion.id,
questionSimilarId: e.questionNumber,
mistakeAnswerId: e.id,
targetExerciseId: e.targetExerciseId,
status: recommendQuestion.status,
}
})
return questionListParsed
}
questionListParsed 主要取决于状态 questionList 是来自服务器端的原始数据。现在我通过以下方式更改 questionList
@Mutation
updateQuestionListByIndex(data: UpdateParams): void {
if (data.value) {
const temp = [...this.questionList]
temp[data.index] = data.value
this.questionList = temp
}
}
并在这样的操作中提交突变
this.context.commit('updateQuestionListByIndex', {
index: targetIndex,
value: originQuestion[0],
})
我只想更改数组 questionList 中的一项,然后 questionListParsed 更改。 预期是只更新了一个组件,但所有子组件都更新了(在其更新的飞节中使用 console.log('updated'))。
怎么做?
【问题讨论】:
标签: vue.js components vue-component vuex v-for