当前位置:首页 > VUE

vue搜索框功能实现

2026-01-20 07:03:05VUE

Vue 搜索框功能实现

基础搜索框实现

创建一个基础的搜索框需要绑定输入框的值到 Vue 的数据属性,并通过事件监听触发搜索逻辑。

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

防抖优化

频繁触发搜索会影响性能,可以通过防抖(debounce)技术优化。

vue搜索框功能实现

import { debounce } from 'lodash'

export default {
  methods: {
    handleSearch: debounce(function() {
      this.filteredItems = this.items.filter(item =>
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }, 300)
  }
}

异步搜索

当需要从 API 获取搜索结果时,可以使用异步方法。

export default {
  methods: {
    async handleSearch() {
      try {
        const response = await axios.get('/api/search', {
          params: { q: this.searchQuery }
        })
        this.filteredItems = response.data
      } catch (error) {
        console.error('搜索失败:', error)
      }
    }
  }
}

高级搜索功能

实现多条件搜索可以扩展搜索逻辑。

vue搜索框功能实现

export default {
  data() {
    return {
      searchOptions: {
        name: '',
        category: '',
        priceRange: [0, 100]
      }
    }
  },
  methods: {
    handleAdvancedSearch() {
      this.filteredItems = this.items.filter(item => {
        const nameMatch = item.name.toLowerCase().includes(
          this.searchOptions.name.toLowerCase()
        )
        const categoryMatch = item.category === this.searchOptions.category
        const priceMatch = item.price >= this.searchOptions.priceRange[0] && 
                         item.price <= this.searchOptions.priceRange[1]
        return nameMatch && categoryMatch && priceMatch
      })
    }
  }
}

搜索历史记录

添加搜索历史功能可以提升用户体验。

export default {
  data() {
    return {
      searchHistory: []
    }
  },
  methods: {
    handleSearch() {
      if (this.searchQuery.trim()) {
        this.searchHistory.unshift(this.searchQuery)
        if (this.searchHistory.length > 5) {
          this.searchHistory.pop()
        }
      }
      // 其他搜索逻辑...
    }
  }
}

样式优化

为搜索框添加样式可以改善视觉效果。

<template>
  <div class="search-container">
    <input 
      class="search-input"
      v-model="searchQuery"
      @input="handleSearch"
      placeholder="输入搜索内容..."
    />
    <div class="search-results">
      <!-- 搜索结果展示 -->
    </div>
  </div>
</template>

<style>
.search-container {
  position: relative;
  width: 300px;
  margin: 20px auto;
}

.search-input {
  width: 100%;
  padding: 10px 15px;
  border: 1px solid #ddd;
  border-radius: 20px;
  outline: none;
}

.search-input:focus {
  border-color: #42b983;
}

.search-results {
  margin-top: 10px;
  border: 1px solid #eee;
  border-radius: 5px;
  max-height: 300px;
  overflow-y: auto;
}
</style>

这些方法涵盖了从基础到高级的 Vue 搜索框实现方式,可以根据实际需求选择适合的方案或组合使用。

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

相关文章

vue实现弹窗可切换

vue实现弹窗可切换

实现弹窗可切换的 Vue 方案 动态组件切换 通过 Vue 的 <component :is="currentComponent"> 动态加载不同弹窗组件,结合 v-if 控制显示状态。…

vue 实现pc

vue 实现pc

Vue 实现 PC 端应用的核心要点 Vue.js 是一个渐进式框架,适合构建 PC 端单页应用(SPA)或复杂的前端界面。以下是实现 PC 端应用的关键方法和注意事项: 项目初始化与工程化配置 使…

vue实现拖放

vue实现拖放

Vue 实现拖放功能 Vue 中实现拖放功能通常可以通过 HTML5 的原生拖放 API 或第三方库(如 vuedraggable)来实现。以下是两种方法的详细说明。 使用 HTML5 原生拖放 A…

vue实现标签

vue实现标签

Vue 实现标签的方法 在 Vue 中实现标签功能可以通过多种方式完成,以下是几种常见的方法: 使用动态组件 动态组件可以根据不同的条件渲染不同的标签内容。通过 :is 属性动态绑定组件名,实现标签…

vue实现oauth

vue实现oauth

Vue 实现 OAuth 的步骤 安装必要的依赖 确保项目中安装了 axios 和 vue-router。OAuth 通常需要发送 HTTP 请求和处理路由跳转。 npm install axios…

vue实现购票

vue实现购票

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