当前位置:首页 > VUE

vue 实现搜索功能

2026-02-17 08:09:21VUE

实现搜索功能的基本思路

在Vue中实现搜索功能通常需要结合数据绑定、计算属性和方法。搜索功能的核心是对数据列表进行过滤,根据用户输入的关键词筛选出匹配的项。

基础实现步骤

数据准备 定义一个数据列表和搜索关键词的绑定变量:

data() {
  return {
    items: [
      { id: 1, name: 'Apple' },
      { id: 2, name: 'Banana' },
      { id: 3, name: 'Orange' }
    ],
    searchQuery: ''
  }
}

计算属性过滤 使用计算属性实现实时搜索过滤:

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

模板绑定 在模板中添加搜索输入框和结果展示:

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

高级搜索功能

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

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

防抖优化 对于频繁触发的搜索,可以使用防抖技术:

import { debounce } from 'lodash'

methods: {
  handleSearch: debounce(function() {
    // 执行搜索逻辑
  }, 300)
}

服务器端搜索

API调用 当数据量较大时,应该使用后端搜索:

methods: {
  async searchItems() {
    try {
      const response = await axios.get('/api/items', {
        params: { q: this.searchQuery }
      })
      this.items = response.data
    } catch (error) {
      console.error(error)
    }
  }
}

监听搜索词变化 使用watch监听searchQuery变化:

watch: {
  searchQuery(newVal) {
    if (newVal.length > 2) {
      this.searchItems()
    }
  }
}

用户体验优化

加载状态 添加加载状态提示:

data() {
  return {
    isLoading: false
  }
}

methods: {
  async searchItems() {
    this.isLoading = true
    try {
      // API调用
    } finally {
      this.isLoading = false
    }
  }
}

空状态提示 在模板中添加空状态提示:

<div v-if="filteredItems.length === 0">
  No items found matching "{{ searchQuery }}"
</div>

完整示例代码

<template>
  <div>
    <input 
      v-model="searchQuery" 
      placeholder="Search..."
      @input="handleSearch"
    >
    <div v-if="isLoading">Loading...</div>
    <ul v-else>
      <li v-for="item in filteredItems" :key="item.id">
        {{ item.name }}
      </li>
    </ul>
    <div v-if="!isLoading && filteredItems.length === 0">
      No results found
    </div>
  </div>
</template>

<script>
import { debounce } from 'lodash'

export default {
  data() {
    return {
      items: [],
      searchQuery: '',
      isLoading: false
    }
  },
  computed: {
    filteredItems() {
      const query = this.searchQuery.toLowerCase()
      return this.items.filter(item => 
        item.name.toLowerCase().includes(query)
      )
    }
  },
  methods: {
    handleSearch: debounce(function() {
      this.searchItems()
    }, 300),
    async searchItems() {
      if (this.searchQuery.length < 2) return

      this.isLoading = true
      try {
        const response = await axios.get('/api/items', {
          params: { q: this.searchQuery }
        })
        this.items = response.data
      } catch (error) {
        console.error(error)
      } finally {
        this.isLoading = false
      }
    }
  }
}
</script>

vue 实现搜索功能

标签: 搜索功能vue
分享给朋友:

相关文章

vue 实现长列表

vue 实现长列表

Vue 实现长列表的优化方案 虚拟滚动技术 虚拟滚动是处理长列表的高效方式,仅渲染可视区域内的元素。通过计算滚动位置动态加载和卸载DOM节点,大幅减少内存占用和渲染压力。 <template&…

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组件…

vue实现生成二维码

vue实现生成二维码

使用qrcode.vue库生成二维码 安装qrcode.vue库: npm install qrcode.vue --save 在Vue组件中使用: <template> &l…

vue实现星星

vue实现星星

实现星星评分的Vue组件 使用Vue实现星星评分功能可以通过组件化方式完成,以下是一个完整的实现方案: 模板部分 <template> <div class="star-ra…

vue实现功能

vue实现功能

Vue 功能实现方法 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现常见功能的几种方法: 数据绑定与响应式更新 在 Vue 中,数据绑定通过 v-model…

vue 方法实现

vue 方法实现

在 Vue 中,方法的实现通常通过 methods 选项完成。以下是 Vue 方法实现的核心要点和示例: 基本方法定义 在 Vue 组件中定义方法时,需将函数声明放在 methods 对象内。这些…