按照该网址http://stackabuse.com/single-page-apps-with-vue-js-and-flask-state-management-with-vuex/上的教程进行操作后,我遇到一个问题,即更改存储中的状态后,无法自动调用计算属性中的函数。相关代码如下:
Survey.vue
computed: {
surveyComplete() {
if (this.survey.questions) {
const numQuestions = this.survey.questions.length
const numCompleted = this.survey.questions.filter(q =>q.choice).length
return numQuestions === numCompleted
}
return false
},
survey() {
return this.$store.state.currentSurvey
},
selectedChoice: {
get() {
const question = this.survey.questions[this.currentQuestion]
return question.choice
},
set(value) {
const question = this.survey.questions[this.currentQuestion]
this.$store.commit('setChoice', { questionId: question.id, choice: value })
}
}
}
当选择调查问题中的单选按钮时,selectedChoice将更改商店中的状态。但是不是同时调用surveyComplete方法。有什么问题?预先感谢!
答案 0 :(得分:0)
surveyComplete()方法不会“监视”您的商店,仅当您更改this.survey.questions时,它将被更新。因此,如果您修改商店,那么SurveyComplete内部什么也不会发生。您可以在方法内部使用商店。