【发布时间】:2020-04-30 18:51:57
【问题描述】:
我想在 web 中显示我的 getter,但在 console.log 中我有未定义的错误。但在 vue 开发工具中,我的 getter 正在工作。
我的组件代码:
export default {
data: () => ({
items: [
{ text: 'Real-Time', icon: 'mdi-clock' },
{ text: 'Audience', icon: 'mdi-account' },
{ text: 'Conversions', icon: 'mdi-flag' },
],
}),
computed: {
Question(){
return this.$store.getters.getQuest
}
},
methods: {
Next(){
this.$store.commit('incrementIndex')
}
},
}
这是我的 vuex :
export default new Vuex.Store({
state: {
question: [],
index: 0
},
getters: {
getQuest: state => {
let a = state.question[state.index]
let answer = [...a.incorrect_answers, a.correct_answer]
return {
question: a,
answer: answer
}
}
},
【问题讨论】:
-
你能添加你得到的错误
-
@Vincent,请尝试提供足够的信息,以便其他人正确理解您的问题。
-
@Himanshu 我只是上传错误图片
-
在盲目访问它的索引之前检查你的getter中
state.question的长度。如果为空,返回一个合适的默认值 -
@Vincent 您能否将 getter 中的 question 数组的值打印到控制台,看看它是否定义了不正确的答案?如果这是问题,只需检查您的吸气剂。