当前位置:首页 > 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 的 v-model 指令绑定数据,并通过 type="password" 设置输入类型为密码: <template> &…

vue前端实现搜索

vue前端实现搜索

实现搜索功能的基本方法 在Vue中实现搜索功能通常涉及以下几个关键步骤,结合数据绑定、计算属性和方法调用来动态过滤和显示结果。 数据绑定与输入处理 使用v-model双向绑定搜索输入框的值,监听用户…

vue如何实现到期提醒

vue如何实现到期提醒

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

vue实现多选题

vue实现多选题

Vue实现多选题的方法 使用Vue实现多选题功能,可以通过v-model绑定数组、动态渲染选项、以及处理选中状态来实现。以下是一个完整的实现示例: 基础实现代码 <template>…

vue实现双折线图

vue实现双折线图

实现双折线图的步骤 安装必要的依赖库(如 ECharts 或 Chart.js),这里以 ECharts 为例: npm install echarts --save 在 Vue 组件中引入 EC…

vue调用接口实现退出

vue调用接口实现退出

实现Vue退出功能的步骤 在Vue中调用接口实现退出功能通常需要与后端API交互,以下是实现方法: 前端实现 创建退出方法,调用后端API接口: methods: { logout()…