当前位置:首页 > VUE

vue如何实现搜索过滤

2026-02-22 23:48:58VUE

Vue 实现搜索过滤的方法

使用计算属性过滤数据

计算属性可以根据搜索关键词动态过滤数组数据,适合处理本地数据。

<template>
  <input v-model="searchQuery" placeholder="搜索...">
  <ul>
    <li v-for="item in filteredItems" :key="item.id">
      {{ item.name }}
    </li>
  </ul>
</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 监听搜索变化

当需要执行异步操作或复杂逻辑时,可以使用 watch 监听搜索词变化。

<script>
export default {
  data() {
    return {
      searchQuery: '',
      filteredItems: [],
      allItems: [
        { id: 1, name: '苹果' },
        { id: 2, name: '香蕉' },
        { id: 3, name: '橙子' }
      ]
    }
  },
  watch: {
    searchQuery(newVal) {
      this.filteredItems = this.allItems.filter(item =>
        item.name.toLowerCase().includes(newVal.toLowerCase())
      )
    }
  },
  created() {
    this.filteredItems = [...this.allItems]
  }
}
</script>

结合 Vuex 实现全局搜索

在大型应用中,可以使用 Vuex 管理搜索状态和过滤逻辑。

vue如何实现搜索过滤

// store.js
const store = new Vuex.Store({
  state: {
    searchQuery: '',
    items: [
      { id: 1, name: '苹果' },
      { id: 2, name: '香蕉' },
      { id: 3, name: '橙子' }
    ]
  },
  getters: {
    filteredItems: state => {
      return state.items.filter(item =>
        item.name.toLowerCase().includes(state.searchQuery.toLowerCase())
      )
    }
  },
  mutations: {
    updateSearchQuery(state, query) {
      state.searchQuery = query
    }
  }
})

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

对于复杂搜索需求,可以集成如 Fuse.js 这类模糊搜索库。

import Fuse from 'fuse.js'

// 在组件中
methods: {
  setupFuse() {
    const options = {
      keys: ['name', 'description'],
      threshold: 0.4
    }
    this.fuse = new Fuse(this.allItems, options)
  },
  search() {
    this.filteredItems = this.searchQuery 
      ? this.fuse.search(this.searchQuery).map(r => r.item)
      : [...this.allItems]
  }
},
created() {
  this.setupFuse()
}

服务端搜索实现

当数据量很大时,应该将搜索逻辑放在服务端。

vue如何实现搜索过滤

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() {
    this.searchItems()
  }
}

性能优化技巧

添加防抖函数避免频繁触发搜索,适合处理实时搜索场景。

import { debounce } from 'lodash'

methods: {
  search: debounce(function() {
    // 搜索逻辑
  }, 300)
}

使用虚拟滚动处理大量搜索结果,提升渲染性能。

<template>
  <RecycleScroller
    :items="filteredItems"
    :item-size="50"
    key-field="id"
    v-slot="{ item }"
  >
    <div>{{ item.name }}</div>
  </RecycleScroller>
</template>

多条件筛选实现

扩展搜索功能支持多个筛选条件。

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

标签: 如何实现vue
分享给朋友:

相关文章

vue设计与实现下载

vue设计与实现下载

vue设计与实现电子书下载 《Vue.js设计与实现》是一本深入解析Vue.js框架原理的书籍,由霍春阳(HcySunYang)撰写。以下是获取该资源的常见途径: 正版购买渠道 京东、当当、天猫等…

vue 实现toast

vue 实现toast

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

vue实现帖子

vue实现帖子

Vue 实现帖子功能 使用 Vue 实现帖子功能需要结合前端框架和后端数据交互,以下是实现的关键步骤和代码示例。 帖子列表展示 创建帖子列表组件,使用 v-for 渲染帖子数据,通过 axios 获…

vue实现alert

vue实现alert

实现自定义 Alert 组件 在 Vue 中可以通过组件化方式实现自定义 Alert 弹窗。以下是基于 Vue 3 的实现示例: 组件代码 (Alert.vue) <template>…

vue怎么实现

vue怎么实现

Vue 实现方法 Vue 是一个流行的前端框架,用于构建用户界面。以下是几种常见的实现方法: 数据绑定 使用 v-model 指令实现双向数据绑定,适用于表单输入元素。 <input v-m…

vue 实现脚本

vue 实现脚本

Vue 实现脚本的方法 Vue.js 提供了多种方式来实现脚本功能,包括组件内脚本、混入(Mixins)、插件(Plugins)以及自定义指令等。以下是常见的实现方式: 组件内脚本 在 Vue 单文…