【发布时间】:2021-02-26 17:19:40
【问题描述】:
我有一个组件QuestionContainer.vue 有几个问题(输入表单)。使用名为 keyUpRoutine(questionkey) 的方法对每个用户给出的答案(用户输入)进行实时验证 (@keyup.prevent="keyUpRoutine(questionkey)")。如果所有答案都有效,我会执行一致性检查:
在 QuestionContainer.vue 中:
keyUpRoutine(questionkey) {
var value = event.target.value;
var question = this.questions[questionkey];
question.validated = this.validate(question, value) ? true : false;
this.allConditioncomplied()
? this.$store.dispatch("checkObligationConsistency", { someData })
: this.questionState = 'default';
// this.questionState must be changed by Vuex' action (app.js) checkObligationConsistency()
}
app.js 中的操作:
checkObligationConsistency(context, obligation) {
context.commit("SET_OBLIGATION_STATE", "checking");
axios
.post("/DataCheck/checkObligationConsistency", obligation)
.then(function(response) {
context.commit("SET_OBLIGATION_STATE", "valid");
if (store.state.modalType == "QuestionPack") {
context.commit("SET_QUESTION_STATE", "add");
// In QuestionContainer.vue, this.questionState must be set to 'add'
// Incorrect approach: store.state.questionState = "add";
} else {
context.commit("SET_QUESTION_STATE", "submit");
// In QuestionContainer.vue, this.questionState must be set to 'submit'
// Incorrect approach: store.state.questionState = "submit";
}
})
.catch(function(error) {
console.log(error);
context.commit("SET_OBLIGATION_STATE", "invalid");
});
}
问题的症结:组件QuestionContainer.vue可能存在两次(常规,有时在模态div中),因此使用Vuex状态将不起作用,因为必须在每个组件中隔离状态.
有没有办法返回 QuestionContainer.vue 的 questionState 的新值并将其封装在每个组件中?
【问题讨论】:
标签: javascript vue.js axios vue-component vuex