当前位置:首页 > VUE

实现模糊查询vue

2026-01-19 15:48:46VUE

实现模糊查询的方法

在Vue中实现模糊查询通常需要结合输入框和列表渲染,通过监听输入框的变化实时过滤数据。以下是几种常见的实现方式:

使用计算属性过滤数据

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索...">
    <ul>
      <li v-for="item in filteredItems" :key="item.id">
        {{ item.name }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [
        { id: 1, name: '苹果' },
        { id: 2, name: '香蕉' },
        { id: 3, name: '橙子' }
      ]
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => 
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

使用lodash的debounce优化性能

对于大数据量或频繁输入的情况,可以使用lodash的debounce函数减少计算频率:

实现模糊查询vue

import { debounce } from 'lodash'

export default {
  data() {
    return {
      searchQuery: '',
      filteredItems: [],
      items: [...]
    }
  },
  watch: {
    searchQuery: debounce(function(newVal) {
      this.filteredItems = this.items.filter(item =>
        item.name.toLowerCase().includes(newVal.toLowerCase())
      )
    }, 300)
  }
}

使用第三方库实现高级搜索

Fuse.js是一个强大的模糊搜索库,可以处理更复杂的搜索需求:

import Fuse from 'fuse.js'

export default {
  data() {
    return {
      searchQuery: '',
      items: [...],
      fuse: null
    }
  },
  created() {
    this.fuse = new Fuse(this.items, {
      keys: ['name', 'description'],
      threshold: 0.3
    })
  },
  computed: {
    filteredItems() {
      return this.searchQuery 
        ? this.fuse.search(this.searchQuery).map(r => r.item)
        : this.items
    }
  }
}

实现多字段搜索

如果需要搜索多个字段,可以扩展过滤逻辑:

实现模糊查询vue

computed: {
  filteredItems() {
    const query = this.searchQuery.toLowerCase()
    return this.items.filter(item => 
      item.name.toLowerCase().includes(query) ||
      item.description.toLowerCase().includes(query)
    )
  }
}

使用自定义过滤器

Vue 2.x中可以注册全局过滤器实现搜索:

Vue.filter('search', function(items, query) {
  if (!query) return items
  return items.filter(item => 
    item.name.toLowerCase().includes(query.toLowerCase())
  )
})

然后在模板中使用:

<ul>
  <li v-for="item in items | search(searchQuery)" :key="item.id">
    {{ item.name }}
  </li>
</ul>

以上方法可以根据具体需求选择使用,计算属性方式适合简单场景,Fuse.js适合复杂搜索需求,debounce优化适合性能敏感场景。

标签: 模糊vue
分享给朋友:

相关文章

vue实现贴图

vue实现贴图

Vue 实现贴图功能 在Vue中实现贴图功能通常涉及将图片动态添加到指定位置或元素上。以下是几种常见实现方式: 使用v-bind动态绑定图片 通过数据驱动的方式动态更换图片源: <templ…

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…

vue实现链接

vue实现链接

Vue 实现链接的方法 在 Vue 中实现链接可以通过多种方式,包括使用 <router-link>、<a> 标签或编程式导航。以下是几种常见的方法: 使用 <rout…

vue实现api

vue实现api

Vue 实现 API 调用 在 Vue 中调用 API 通常涉及使用 axios 或 fetch 发送 HTTP 请求,并结合 Vue 的响应式特性管理数据。以下是常见的实现方式: 安装 axios…

vue 实现折叠

vue 实现折叠

Vue 实现折叠功能的方法 在 Vue 中实现折叠功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 v-if 或 v-show 控制显示隐藏 通过绑定一个布尔值变量,结合 v-if 或 v…

vue怎么实现onclick

vue怎么实现onclick

在Vue中实现点击事件(类似原生onclick)可以通过多种方式完成,以下是常见方法: 使用 v-on 指令 Vue提供了v-on指令绑定DOM事件,简写为@。例如绑定点击事件: <butt…