当前位置:首页 > VUE

vue 实现搜索功能

2026-03-29 07:34:19VUE

实现基础搜索功能

在Vue中实现搜索功能可以通过v-model绑定输入框,结合计算属性或方法过滤数据。以下是一个基础实现示例:

<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>

使用防抖优化性能

频繁触发搜索会导致性能问题,可以使用lodash的debounce函数实现防抖:

import { debounce } from 'lodash'

export default {
  data() {
    return {
      searchQuery: '',
      filteredItems: []
    }
  },
  watch: {
    searchQuery: debounce(function(newVal) {
      this.filterItems(newVal)
    }, 300)
  },
  methods: {
    filterItems(query) {
      this.filteredItems = this.items.filter(item =>
        item.name.toLowerCase().includes(query.toLowerCase())
      )
    }
  }
}

实现多字段搜索

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

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

结合后端API搜索

对于大量数据,建议使用后端搜索API:

methods: {
  async searchItems() {
    try {
      const response = await axios.get('/api/items', {
        params: { q: this.searchQuery }
      })
      this.filteredItems = response.data
    } catch (error) {
      console.error('搜索失败:', error)
    }
  }
},
watch: {
  searchQuery(newVal) {
    if (newVal.length > 2 || newVal.length === 0) {
      this.searchItems()
    }
  }
}

添加搜索建议

实现自动完成搜索建议功能:

<template>
  <div>
    <input 
      v-model="searchQuery" 
      @input="getSuggestions"
      @keydown.down="highlightNext"
      @keydown.up="highlightPrev"
      @keydown.enter="selectSuggestion"
    >
    <ul v-if="suggestions.length">
      <li 
        v-for="(suggestion, index) in suggestions"
        :key="index"
        :class="{ highlighted: index === highlightedIndex }"
        @click="selectSuggestion(index)"
      >
        {{ suggestion }}
      </li>
    </ul>
  </div>
</template>

实现高级搜索过滤器

添加多个搜索条件作为过滤器:

vue 实现搜索功能

<template>
  <div>
    <input v-model="filters.name" placeholder="名称">
    <input v-model="filters.category" placeholder="分类">
    <input type="number" v-model.number="filters.minPrice" placeholder="最低价格">

    <ul>
      <li v-for="item in filteredItems" :key="item.id">
        {{ item.name }} - {{ item.price }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      filters: {
        name: '',
        category: '',
        minPrice: 0
      },
      items: [] // 你的数据
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => {
        return (
          item.name.toLowerCase().includes(this.filters.name.toLowerCase()) &&
          item.category.toLowerCase().includes(this.filters.category.toLowerCase()) &&
          item.price >= this.filters.minPrice
        )
      })
    }
  }
}
</script>

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

相关文章

前端多线程实现vue

前端多线程实现vue

前端多线程与Vue的结合 在Vue中实现多线程通常通过Web Worker技术完成。Web Worker允许在后台线程中运行脚本,避免阻塞主线程,适合处理计算密集型任务。 使用Web Worker的…

vue懒加载实现难吗

vue懒加载实现难吗

vue懒加载的实现难度 Vue懒加载的实现并不复杂,核心逻辑是通过动态导入(Dynamic Imports)和路由配置或组件异步加载完成。以下是具体实现方法: 路由懒加载实现 在Vue Router…

vue 实现toast

vue 实现toast

vue 实现 toast 的方法 在 Vue 中实现 Toast 提示功能可以通过多种方式,以下是几种常见的实现方法: 使用第三方库 Vue 生态中有许多成熟的 Toast 库,例如 vue-toa…

ssh vue 实现

ssh vue 实现

使用SSH连接Vue项目部署的服务器 在Vue项目开发或部署过程中,可能需要通过SSH连接到远程服务器进行配置或维护。以下是实现SSH连接并部署Vue项目的关键步骤: 生成SSH密钥对 在本地终端运…

vue实现骰子

vue实现骰子

Vue 实现骰子功能 创建 Vue 组件 在 Vue 项目中创建一个骰子组件,例如 Dice.vue。该组件包含骰子的外观和逻辑。 <template> <div class=…

vue实现购票

vue实现购票

Vue 实现购票功能 数据结构设计 购票功能通常需要以下数据结构: 场次信息(时间、地点、价格) 座位信息(可选座位、已售座位) 用户订单信息 示例数据结构: data() { return…