Vue实现搜索框模糊查询的方法有以下几种:
computed: {
filteredData() {
return this.dataList.filter(item => item.name.includes(this.keyword));
}
}
watch: {
keyword: {
handler(newKeyword) {
this.filteredData = this.dataList.filter(item => item.name.includes(newKeyword));
},
immediate: true
}
}
Vue.directive('filter', {
bind(el, binding) {
el.addEventListener('input', function() {
const keyword = el.value;
binding.value(keyword);
});
},
update(el, binding) {
const keyword = el.value;
binding.value(keyword);
}
});
<template>
<input v-filter="filterData" />
</template>
methods: {
filterData(keyword) {
this.filteredData = this.dataList.filter(item => item.name.includes(keyword));
}
}
以上是一些常见的实现搜索框模糊查询的方法,具体可以根据自己的需求选择适合的方式。