当前位置:首页 > VUE

使用vue实现模糊搜索

2026-02-25 03:16:18VUE

实现模糊搜索的基本思路

在Vue中实现模糊搜索通常需要结合输入框的监听、数据处理和过滤逻辑。模糊搜索的核心是对用户输入的关键词进行模糊匹配,返回相关结果。

安装依赖(可选)

如果需要更强大的模糊搜索功能,可以安装fuse.js库:

npm install fuse.js

基础实现方式

使用Vue的computed属性实现简单模糊搜索:

使用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() {
      const query = this.searchQuery.toLowerCase()
      return this.items.filter(item => 
        item.name.toLowerCase().includes(query)
      )
    }
  }
}
</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: '苹果' },
        { id: 2, name: '香蕉' },
        { id: 3, name: '橙子' }
      ],
      fuse: null
    }
  },
  created() {
    this.fuse = new Fuse(this.items, {
      keys: ['name'],
      includeScore: true,
      threshold: 0.4
    })
  },
  computed: {
    searchResults() {
      return this.searchQuery 
        ? this.fuse.search(this.searchQuery) 
        : this.items.map(item => ({ item }))
    }
  }
}
</script>

优化搜索性能

对于大数据集,可以添加防抖功能减少频繁搜索带来的性能问题:

使用vue实现模糊搜索

<script>
import _ from 'lodash'

export default {
  data() {
    return {
      searchQuery: '',
      items: [...], // 大量数据
      filteredItems: []
    }
  },
  watch: {
    searchQuery: _.debounce(function(newVal) {
      this.filteredItems = this.items.filter(item =>
        item.name.toLowerCase().includes(newVal.toLowerCase())
      )
    }, 300)
  }
}
</script>

多字段搜索实现

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

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

样式优化建议

为搜索结果添加高亮显示,提升用户体验:

<template>
  <li v-for="item in filteredItems" :key="item.id">
    <span v-html="highlight(item.name, searchQuery)"></span>
  </li>
</template>

<script>
export default {
  methods: {
    highlight(text, query) {
      if (!query) return text
      const regex = new RegExp(query, 'gi')
      return text.replace(regex, match => `<span class="highlight">${match}</span>`)
    }
  }
}
</script>

<style>
.highlight {
  background-color: yellow;
  font-weight: bold;
}
</style>

这些方法可以根据实际项目需求进行组合和调整,实现适合不同场景的模糊搜索功能。

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

相关文章

vue实现按钮组轮换

vue实现按钮组轮换

实现按钮组轮换的方法 在Vue中实现按钮组轮换效果可以通过动态绑定类和事件处理来完成。以下是一种常见的实现方式: 模板部分 <template> <div class="bu…

vue实现打印二维码

vue实现打印二维码

安装二维码生成库 使用 qrcode 或 vue-qrcode 库生成二维码。通过 npm 安装: npm install qrcode vue-qrcode 生成二维码 在 Vue 组…

vue实现数组

vue实现数组

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

vue实现翻页

vue实现翻页

Vue实现翻页功能 在Vue中实现翻页功能通常需要结合分页组件和数据请求逻辑。以下是常见的实现方法: 使用Element UI分页组件 安装Element UI后,可以直接使用其分页组件:…

vue递归实现

vue递归实现

vue递归实现方法 在Vue中实现递归组件通常用于树形结构、嵌套菜单等场景。以下是具体实现方式: 使用组件name属性递归 定义一个组件时,通过name属性让组件可以在自身模板中调用自己:…

vue博客实现

vue博客实现

Vue 博客实现步骤 项目初始化与基础配置 使用 Vue CLI 或 Vite 创建项目,安装基础依赖如 vue-router 和状态管理库(如 pinia)。配置路由文件实现页面跳转逻辑,例如博客首…