当前位置:首页 > VUE

vue实现模糊查找

2026-01-15 04:00:28VUE

实现模糊查找的方法

在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: 'Apple' },
        { id: 2, name: 'Banana' },
        { id: 3, name: 'Orange' }
      ]
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => 
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

使用第三方库(如Fuse.js)

对于更复杂的模糊搜索需求,可以使用Fuse.js这类模糊搜索库。它支持权重、模糊匹配等高级功能。

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

<script>
import Fuse from 'fuse.js'

export default {
  data() {
    return {
      searchQuery: '',
      items: [
        { id: 1, name: 'Apple' },
        { id: 2, name: 'Banana' },
        { id: 3, name: 'Orange' }
      ],
      fuse: null
    }
  },
  created() {
    this.fuse = new Fuse(this.items, {
      keys: ['name'],
      threshold: 0.4
    })
  },
  computed: {
    searchResults() {
      return this.searchQuery ? this.fuse.search(this.searchQuery) : this.items.map(item => ({ item }))
    }
  }
}
</script>

使用自定义过滤器

Vue 2.x支持自定义过滤器,可以创建一个模糊搜索过滤器(Vue 3.x中过滤器已被移除,需改用方法或计算属性)。

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

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [
        { id: 1, name: 'Apple' },
        { id: 2, name: 'Banana' },
        { id: 3, name: 'Orange' }
      ]
    }
  },
  filters: {
    fuzzySearch(items, query) {
      return items.filter(item => 
        item.name.toLowerCase().includes(query.toLowerCase())
      )
    }
  }
}
</script>

结合防抖优化性能

对于高频输入场景,可以使用防抖(debounce)减少计算频率,提升性能。

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

<script>
import { debounce } from 'lodash'

export default {
  data() {
    return {
      searchQuery: '',
      items: [
        { id: 1, name: 'Apple' },
        { id: 2, name: 'Banana' },
        { id: 3, name: 'Orange' }
      ],
      debouncedFilter: null
    }
  },
  created() {
    this.debouncedFilter = debounce(this.filterItems, 300)
  },
  watch: {
    searchQuery() {
      this.debouncedFilter()
    }
  },
  methods: {
    filterItems() {
      this.filteredItems = this.items.filter(item => 
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

注意事项

  • 数据量较大时,建议使用Web Worker或服务端搜索以减少主线程压力。
  • 模糊搜索的实现可根据需求调整匹配规则,如忽略大小写、部分匹配等。
  • 对于中文搜索,可能需要额外的分词处理以提高准确性。

vue实现模糊查找

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

相关文章

vue实现目录

vue实现目录

Vue 实现目录功能 在 Vue 中实现目录功能通常需要动态生成目录结构,并支持点击跳转到对应内容区域。以下是几种常见实现方式: 基于滚动监听和动态生成 安装依赖(如需): npm inst…

vue实现滚动中断

vue实现滚动中断

Vue 实现滚动中断的方法 在 Vue 中实现滚动中断通常涉及监听滚动事件,并在特定条件下阻止默认行为或停止滚动。以下是几种常见方法: 监听滚动事件并阻止默认行为 通过 @scroll 或 @wh…

vue实现多个tab

vue实现多个tab

Vue 实现多个 Tab 的方法 使用动态组件切换 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现多个…

vue实现横向导航

vue实现横向导航

vue实现横向导航的方法 使用Flex布局实现横向导航 在Vue中可以通过Flex布局快速实现横向导航。创建一个组件,使用display: flex和flex-direction: row属性使导航项…

vue实现查询替换

vue实现查询替换

Vue 实现查询替换功能 在 Vue 中实现查询替换功能,可以通过数据绑定和字符串操作方法结合实现。以下是具体实现方式: 基础实现 <template> <div>…

vue实现本地数据存储

vue实现本地数据存储

使用 localStorage 或 sessionStorage Vue 中可以直接调用浏览器提供的 localStorage 或 sessionStorage API 进行数据存储。localSto…