我最近在为物业网站构建搜索组件方面获得了一些帮助。这个想法是我有一个/properties带有属性列表的页面和一个通用搜索组件,该组件在每个页面上都有一个指向的操作/properties和一个GET方法。
一个示例搜索可能是:
/properties/?search=Green%20Park&type=Apartment&type=Bungalow&bedrooms=1&county=Blaenau%20Gwent
或者...
/properties/?search=&type=Apartment&bedrooms=1&county=
我有一些代码旨在将过滤后的属性显示为计算属性。但是,声明中似乎有问题,else并且没有返回任何内容。这是我的代码:
computed: {
filteredProperties: function () {
let self = this
let routeConstraints = ['search', 'type', 'bedrooms', 'county'].filter(function (val) {
return self.$route.query[val] !== undefined
})
if (routeConstraints.length === 0) {
return self.properties
} else {
return routeConstraints.reduce(function (acc, val) {
return acc.filter(function (property) {
//basically I am checking if there is some value in the query parameter that matches properties.
return self.$route.query[val].some(function (item) {
//changed the matching condition to indexOf
return property[val].match(item).length > 0
})
})
}, self.properties)
}
}
}
我收到以下错误:
self.$route.query[val].some is not a function
但它只会在尝试使用搜索时出错(参见上面的示例),如果我不使用搜索并直接进入属性页面,它就可以正常工作:/properties.
类型:可以是单一类型,也可以是多个类型。
谁能帮助我并解释为什么它给我这个错误以及如何解决它?谢谢!