computed: {
filteredQuestions: function () {
return this.allquestions.filter((question) => {
return question.question.match(this.search)
})
}
},
我有上面的计算属性,我在以下方式获取创建方法中的数据:
created() {
axios.get('/teacher/quiz/questions')
.then((response) => {
console.log(response.data);
this.allquestions = response.data;
})
.catch(function (error) {
console.log(error);
});
},
答案 0 :(得分:1)
axios.get
是异步操作。
所以,当程序第一次运行时,this.allquestions
实际上仍然是undefined
,并且您的计算方法失败。
我会在allquestions
属性上将前端data
声明为空数组。
参见代码:
<script>
export default {
data() {
return {
allquestions: []
}
},
computed: {
filteredQuestions: function() {
return this.allquestions.filter(question => {
return question.question.match(this.search)
})
}
},
created() {
axios
.get('/teacher/quiz/questions')
.then(response => {
console.log(response.data)
this.allquestions = response.data
})
.catch(function(error) {
console.log(error)
})
}
}
</script>