当前位置:首页 > VUE

实现vue前端搜索

2026-01-16 08:00:50VUE

实现Vue前端搜索功能

在Vue中实现前端搜索功能可以通过多种方式完成,包括使用计算属性、过滤器或第三方库。以下是一些常见的方法:

使用计算属性实现搜索

计算属性是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>

使用watch和methods实现搜索

对于更复杂的搜索逻辑,可以使用watch和methods组合实现。

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索...">
    <ul>
      <li v-for="item in searchResults" :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: '橙子' }
      ],
      searchResults: []
    }
  },
  watch: {
    searchQuery(newVal) {
      this.searchItems(newVal)
    }
  },
  methods: {
    searchItems(query) {
      this.searchResults = this.items.filter(item =>
        item.name.toLowerCase().includes(query.toLowerCase())
      )
    }
  },
  created() {
    this.searchResults = [...this.items]
  }
}
</script>

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

对于更复杂的搜索需求,可以使用第三方库如Fuse.js实现模糊搜索。

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索...">
    <ul>
      <li v-for="item in searchResults" :key="item.id">
        {{ 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: '橙子' }
      ],
      searchResults: [],
      fuse: null
    }
  },
  created() {
    this.fuse = new Fuse(this.items, {
      keys: ['name'],
      threshold: 0.4
    })
    this.searchResults = [...this.items]
  },
  watch: {
    searchQuery(newVal) {
      if (newVal.trim() === '') {
        this.searchResults = [...this.items]
      } else {
        this.searchResults = this.fuse.search(newVal).map(result => result.item)
      }
    }
  }
}
</script>

实现防抖搜索优化性能

对于频繁触发的搜索输入,可以使用防抖函数优化性能。

实现vue前端搜索

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

<script>
import { debounce } from 'lodash'

export default {
  data() {
    return {
      searchQuery: '',
      items: [
        { id: 1, name: '苹果' },
        { id: 2, name: '香蕉' },
        { id: 3, name: '橙子' }
      ],
      searchResults: []
    }
  },
  created() {
    this.debouncedSearch = debounce(this.searchItems, 300)
    this.searchResults = [...this.items]
  },
  watch: {
    searchQuery(newVal) {
      this.debouncedSearch(newVal)
    }
  },
  methods: {
    searchItems(query) {
      this.searchResults = this.items.filter(item =>
        item.name.toLowerCase().includes(query.toLowerCase())
      )
    }
  }
}
</script>

这些方法可以根据具体需求选择使用,从简单的计算属性过滤到复杂的模糊搜索和性能优化方案。

标签: vue
分享给朋友:

相关文章

vue实现123456

vue实现123456

Vue 实现数字 123456 的显示 在 Vue 中显示数字 123456 非常简单,可以通过以下方法实现: 方法一:直接在模板中显示 <template> <div>…

vue实现刷卡

vue实现刷卡

Vue 实现刷卡效果 在 Vue 中实现刷卡效果可以通过 CSS 动画和 Vue 的过渡系统结合完成。以下是一种常见的实现方式: 基础实现方法 安装 Vue 过渡依赖(如果尚未安装): npm i…

vue 实现图钉

vue 实现图钉

实现图钉效果的步骤 在Vue中实现图钉(固定位置)效果,可以通过CSS的position: sticky属性或监听滚动事件动态修改元素样式。以下是两种常见方法: 方法一:使用CSS的position…

vue实现折叠

vue实现折叠

Vue 实现折叠功能 在 Vue 中实现折叠功能可以通过多种方式完成,以下介绍几种常见的方法。 使用 v-show 或 v-if 控制显示隐藏 通过 Vue 的指令 v-show 或 v-if 可以…

vue实现tip

vue实现tip

Vue实现Tooltip的方法 使用Vue实现Tooltip可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML的title属性 在Vue模板中直接使用HTML的title属性是最简单的实…

vue router实现分页

vue router实现分页

Vue Router 实现分页的方法 在 Vue.js 中,可以通过 Vue Router 实现分页功能,通常需要结合路由参数和动态路由匹配。以下是几种常见的实现方式: 使用查询参数实现分页 在路由…