当前位置:首页 > VUE

vue 搜索功能实现

2026-01-16 22:48:37VUE

实现 Vue 搜索功能的基本步骤

在 Vue 中实现搜索功能通常需要结合数据绑定、计算属性和方法。以下是几种常见的实现方式:

基于计算属性的搜索

<template>
  <div>
    <input v-model="searchQuery" placeholder="Search...">
    <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: 'Apple' },
        { id: 2, name: 'Banana' },
        { id: 3, name: 'Orange' }
      ]
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => 
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

使用 watch 实现搜索

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [
        { id: 1, name: 'Apple' },
        { id: 2, name: 'Banana' },
        { id: 3, name: 'Orange' }
      ],
      filteredItems: []
    }
  },
  watch: {
    searchQuery(newVal) {
      this.filteredItems = this.items.filter(item =>
        item.name.toLowerCase().includes(newVal.toLowerCase())
      )
    }
  },
  created() {
    this.filteredItems = [...this.items]
  }
}
</script>

结合 API 请求的搜索

对于从后端获取数据的搜索功能:

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [],
      loading: false
    }
  },
  methods: {
    async searchItems() {
      this.loading = true
      try {
        const response = await axios.get('/api/items', {
          params: {
            q: this.searchQuery
          }
        })
        this.items = response.data
      } catch (error) {
        console.error(error)
      } finally {
        this.loading = false
      }
    }
  },
  watch: {
    searchQuery() {
      this.searchItems()
    }
  }
}
</script>

使用防抖优化搜索性能

为防止频繁触发搜索请求,可以添加防抖功能:

<script>
import _ from 'lodash'

export default {
  data() {
    return {
      searchQuery: '',
      items: []
    }
  },
  created() {
    this.debouncedSearch = _.debounce(this.searchItems, 500)
  },
  watch: {
    searchQuery() {
      this.debouncedSearch()
    }
  },
  methods: {
    async searchItems() {
      const response = await axios.get('/api/items', {
        params: {
          q: this.searchQuery
        }
      })
      this.items = response.data
    }
  }
}
</script>

多条件搜索实现

对于需要多个搜索条件的场景:

<template>
  <div>
    <input v-model="searchParams.name" placeholder="Name">
    <input v-model="searchParams.category" placeholder="Category">
    <ul>
      <li v-for="item in filteredItems" :key="item.id">
        {{ item.name }} - {{ item.category }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchParams: {
        name: '',
        category: ''
      },
      items: [
        { id: 1, name: 'Apple', category: 'Fruit' },
        { id: 2, name: 'Carrot', category: 'Vegetable' }
      ]
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => {
        const nameMatch = item.name.toLowerCase().includes(
          this.searchParams.name.toLowerCase()
        )
        const categoryMatch = item.category.toLowerCase().includes(
          this.searchParams.category.toLowerCase()
        )
        return nameMatch && categoryMatch
      })
    }
  }
}
</script>

以上实现方式可以根据具体需求进行调整和组合,构建适合项目的搜索功能。

vue 搜索功能实现

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

相关文章

vue实现预览

vue实现预览

Vue实现文件预览的方法 使用Vue实现文件预览功能可以通过多种方式完成,具体取决于文件类型(图片、PDF、视频等)。以下是几种常见文件类型的实现方案。 图片预览 通过<input type…

vue如何实现到期提醒

vue如何实现到期提醒

实现 Vue 到期提醒功能 使用计算属性计算剩余时间 在 Vue 组件中定义一个计算属性,计算目标日期与当前日期的差值。通过 Date 对象获取时间戳并计算剩余天数或小时数。 computed: {…

vue实现微博印象

vue实现微博印象

实现微博印象功能的核心思路 微博印象功能通常指用户发布微博后,系统自动分析文本内容并提取关键词或标签,以可视化形式展示。Vue实现该功能需要结合文本分析、关键词提取和前端渲染技术。 文本分析与关键…

vue状态管理怎么实现

vue状态管理怎么实现

Vue 状态管理实现方法 使用 Vuex(官方推荐) Vuex 是 Vue 的官方状态管理库,适合中大型应用。 安装 Vuex: npm install vuex --save 创建 Store 示…

vue实现文章锚点定位

vue实现文章锚点定位

实现文章锚点定位的方法 在Vue中实现文章锚点定位可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML锚点 通过HTML的id属性和<a>标签的href属性实现锚点跳转。在Vu…

vue前端实现下载进度

vue前端实现下载进度

实现下载进度条的基本思路 在Vue中实现下载进度条,通常需要结合XMLHttpRequest或Fetch API来监听下载进度事件。通过计算已下载数据与总数据的比例,动态更新进度条的显示。 使用XM…