Javascript vuejs筛选器函数显示空数组
我创建了一个过滤器函数,我想显示结果。这是密码Javascript vuejs筛选器函数显示空数组,javascript,vue.js,vuejs2,Javascript,Vue.js,Vuejs2,我创建了一个过滤器函数,我想显示结果。这是密码 data() { return { questionList: faqData.flatMap(q => q.questions) } }, computed: { search() { return this.$store.state.search }, filter: function()
data() {
return {
questionList: faqData.flatMap(q => q.questions)
}
},
computed: {
search() {
return this.$store.state.search
},
filter: function() {
this.questionList.filter((x) => {
return x.question.match(this.search);
})
}
}
questionList
变量和search()
函数没有问题。我认为问题出在filter()
函数中。不管怎样,这是我的问题清单
[{id: 1, question: 'blabla', answer: 'blabalbla'}, {id: 2, question: 'blabla', answer: 'blabalbla'}, {id: 3, question: 'blabla', answer: 'blabalbla'} ]
filter函数显示一个空数组。谁能告诉我哪里做错了?谢谢 计算属性的值由其函数返回的值定义 在本例中,
未定义
,因为过滤器:函数(){
不返回任何内容
在计算属性函数中指定状态变量根本不是一个好主意!
正确的方法是:
filter: function() {
return this.questionList.filter((question) => {
return question.title.match(this.search);
})
}
{{项目.问题}
导出默认值{
数据(){
返回{
问题列表:faqData.flatMap(q=>q.questions)
}
},
计算:{
搜索(){
返回此项。$store.state.search;
},
filt:function(){
返回此.questionList.filter((x)=>{
返回x.question.match(this.search);
})
}
}
}
为什么要将计算出的函数保存到变量中?this.result
和this.filter
是完全相同的。感谢您的解释!我以前确实编写了该代码,但没有保存名为result的变量。但问题是我想循环此筛选函数的结果,就像这样sv-for=“x in filter”
但它不起作用。有什么建议吗?它怎么不起作用?你能链接到复制品吗?
<template>
<div class="hello">
<div>
<p v-for="(item, index) in filt" :key="index">
{{item.question}}
</p>
</div>
</div>
</template>
<script>
export default {
data() {
return {
questionList: faqData.flatMap(q => q.questions)
}
},
computed: {
search() {
return this.$store.state.search;
},
filt: function() {
return this.questionList.filter((x) => {
return x.question.match(this.search);
})
}
}
}
</script>