当前位置:首页 > VUE

vue搜索功能代码实现

2026-02-25 03:46:27VUE

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>

异步搜索(API请求)

对于需要从API获取搜索结果的场景,可以使用防抖技术优化性能。

vue搜索功能代码实现

<template>
  <div>
    <input 
      v-model="searchQuery" 
      @input="debouncedSearch" 
      placeholder="搜索..."
    />
    <ul v-if="searchResults.length">
      <li v-for="result in searchResults" :key="result.id">
        {{ result.title }}
      </li>
    </ul>
    <p v-else-if="isLoading">加载中...</p>
    <p v-else>暂无结果</p>
  </div>
</template>

<script>
import _ from 'lodash'

export default {
  data() {
    return {
      searchQuery: '',
      searchResults: [],
      isLoading: false
    }
  },
  created() {
    this.debouncedSearch = _.debounce(this.searchAPI, 500)
  },
  methods: {
    async searchAPI() {
      if (!this.searchQuery.trim()) {
        this.searchResults = []
        return
      }

      this.isLoading = true
      try {
        const response = await fetch(`/api/search?q=${encodeURIComponent(this.searchQuery)}`)
        this.searchResults = await response.json()
      } catch (error) {
        console.error('搜索失败:', error)
      } finally {
        this.isLoading = false
      }
    }
  }
}
</script>

高级搜索功能

实现多条件搜索可以结合多个过滤条件。

computed: {
  filteredItems() {
    return this.items.filter(item => {
      const matchesSearch = item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      const matchesCategory = this.selectedCategory ? item.category === this.selectedCategory : true
      return matchesSearch && matchesCategory
    })
  }
}

使用第三方库

对于复杂搜索需求,可以考虑使用专门搜索库如 Fuse.js。

import Fuse from 'fuse.js'

// 在组件中
methods: {
  initFuse() {
    const options = {
      keys: ['name', 'description'],
      threshold: 0.4
    }
    this.fuse = new Fuse(this.items, options)
  },
  searchWithFuse() {
    this.searchResults = this.searchQuery 
      ? this.fuse.search(this.searchQuery)
      : this.items
  }
}

性能优化技巧

对于大型数据集,考虑分页加载结果或虚拟滚动技术。可以使用 Web Worker 将搜索逻辑放到后台线程执行,避免阻塞UI。

分享给朋友:

相关文章

css网页制作代码

css网页制作代码

以下是一些常用的CSS网页制作代码示例,涵盖布局、样式和交互效果: 基础布局与样式 重置浏览器默认样式 * { margin: 0; padding: 0; box-sizing:…

php实现文件下载代码

php实现文件下载代码

实现文件下载的基本方法 使用PHP实现文件下载的核心是通过设置HTTP头部信息,强制浏览器触发下载行为而非直接显示文件内容。 $file_path = 'path/to/your/file.ext…

vue 实现git代码对比

vue 实现git代码对比

Vue 实现 Git 代码对比 在 Vue 中实现 Git 代码对比功能,可以通过集成现有的代码对比库或调用 Git 相关 API 来实现。以下是几种常见的方法: 使用 diff2html 库 d…

vue实现分页搜索功能

vue实现分页搜索功能

实现分页搜索功能 在Vue中实现分页搜索功能通常需要结合前端分页逻辑和后端API(或模拟数据)。以下是一个完整的实现方案: 基础数据准备 创建Vue组件时定义必要的数据属性: data() {…

vue交错实现代码

vue交错实现代码

Vue 交错动画实现方法 使用 Vue 的 <transition-group> 组件结合 CSS 过渡可以实现交错动画效果。以下是一个典型实现方式: <template&…

vue 实现 代码编辑功能

vue 实现 代码编辑功能

实现代码编辑功能的基本思路 在Vue中实现代码编辑功能通常需要借助第三方库,例如Monaco Editor(VS Code使用的编辑器)或CodeMirror。以下是两种常见方案的实现方法。 使…