当前位置:首页 > VUE

vue 搜索功能实现

2026-01-16 22:48:37VUE

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

在 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 请求的搜索

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

vue 搜索功能实现

<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实现流程

Vue 实现流程的基本步骤 安装 Vue.js 可以通过 CDN 引入或使用 npm/yarn 安装。 CDN 方式: <script src="https://cdn.jsdelivr.…

vue 实现拖拽

vue 实现拖拽

Vue 实现拖拽功能 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。 <template> &l…

vue实现绘图

vue实现绘图

Vue 实现绘图的方法 在 Vue 中实现绘图功能,可以通过多种方式实现,包括使用原生 HTML5 Canvas、第三方库(如 Fabric.js、Konva.js)或 SVG。以下是几种常见的实现方…

vue实现dialog

vue实现dialog

Vue 实现 Dialog 的方法 使用 Vue 原生组件实现 Vue 可以通过组件化的方式实现 Dialog,以下是一个简单的实现示例: <template> <div>…

vue实现im通信

vue实现im通信

Vue 实现 IM 通信的方法 选择通信协议 WebSocket 是实现实时 IM 通信的常用协议,相比 HTTP 更适合双向通信。可使用原生 WebSocket 或第三方库如 Socket.IO。…