1,使用场景
列表数据是从vuex中返回的,computed中引入state的值并渲染
模糊搜索功能,点击搜索时发现使用
filter不生效,我搜索的时对象数组,添一下不生效的源代码
search (e) {
this.lost = this.lost.filter((item)=>{
return Object.keys(item).some(function(key) {
return String(item[key]).toLowerCase().indexOf(e.value) > -1
})
});
},
2,发现问题
对vuex中的state的值更改只能通过mutation方法进行更改,像上图中的方法是无效的,所以模糊搜索不生效
3,解决问题
在vuex中的mutation中添加方法
mutations: {
search(state,info){
state.lost = state.lost.filter((item)=>{
return Object.keys(item).some(function(key) {
return String(item[key]).toLowerCase().indexOf(info) > -1
})
});
},
}
然后要需要模糊搜索的页面中进入引入该方法,在模糊搜索中进行调用该方法传入我们在搜索框中输入的值即可,没值的时候重新发起一次异步请求获取数据
search2(e){
if(e.value != ''){
this.search(e.value)
}else{
this.$store.dispatch("getLostList")
}
}