当前位置:首页 > VUE

vue实现搜索组件

2026-02-18 02:29:10VUE

实现搜索组件的基本结构

在Vue中实现搜索组件需要创建一个独立的组件,包含输入框和搜索按钮。以下是一个基础的模板结构:

<template>
  <div class="search-container">
    <input 
      type="text" 
      v-model="searchQuery" 
      placeholder="输入搜索内容..."
      @keyup.enter="handleSearch"
    />
    <button @click="handleSearch">搜索</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: ''
    }
  },
  methods: {
    handleSearch() {
      this.$emit('search', this.searchQuery)
    }
  }
}
</script>

<style scoped>
.search-container {
  display: flex;
  gap: 10px;
}
</style>

添加防抖功能

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

import { debounce } from 'lodash'

export default {
  data() {
    return {
      searchQuery: '',
      debouncedSearch: debounce(this.emitSearch, 500)
    }
  },
  methods: {
    handleSearch() {
      this.debouncedSearch()
    },
    emitSearch() {
      this.$emit('search', this.searchQuery)
    }
  }
}

集成到父组件

在父组件中使用搜索组件并处理搜索事件:

<template>
  <div>
    <search-component @search="performSearch" />
    <!-- 显示搜索结果 -->
    <div v-if="searchResults.length">
      <div v-for="result in searchResults" :key="result.id">
        {{ result.title }}
      </div>
    </div>
  </div>
</template>

<script>
import SearchComponent from './SearchComponent.vue'

export default {
  components: {
    SearchComponent
  },
  data() {
    return {
      searchResults: []
    }
  },
  methods: {
    async performSearch(query) {
      try {
        const response = await fetch(`/api/search?q=${query}`)
        this.searchResults = await response.json()
      } catch (error) {
        console.error('搜索失败:', error)
      }
    }
  }
}
</script>

添加搜索建议

实现搜索建议功能可以提升用户体验:

<template>
  <div class="search-container">
    <input 
      type="text" 
      v-model="searchQuery" 
      @input="fetchSuggestions"
      @focus="showSuggestions = true"
      @blur="hideSuggestions"
    />
    <ul v-if="showSuggestions && suggestions.length" class="suggestions">
      <li 
        v-for="suggestion in suggestions" 
        :key="suggestion"
        @mousedown="selectSuggestion(suggestion)"
      >
        {{ suggestion }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      suggestions: [],
      showSuggestions: false
    }
  },
  methods: {
    async fetchSuggestions() {
      if (this.searchQuery.length > 2) {
        const response = await fetch(`/api/suggestions?q=${this.searchQuery}`)
        this.suggestions = await response.json()
      }
    },
    selectSuggestion(suggestion) {
      this.searchQuery = suggestion
      this.showSuggestions = false
      this.handleSearch()
    },
    hideSuggestions() {
      setTimeout(() => {
        this.showSuggestions = false
      }, 200)
    }
  }
}
</script>

<style scoped>
.suggestions {
  position: absolute;
  list-style: none;
  padding: 0;
  margin: 0;
  background: white;
  border: 1px solid #ddd;
  width: 100%;
  max-height: 200px;
  overflow-y: auto;
}

.suggestions li {
  padding: 8px;
  cursor: pointer;
}

.suggestions li:hover {
  background-color: #f5f5f5;
}
</style>

高级搜索功能

对于更复杂的搜索需求,可以实现高级搜索选项:

<template>
  <div>
    <input v-model="searchQuery" placeholder="基本搜索" />
    <button @click="toggleAdvanced">高级搜索</button>

    <div v-if="showAdvanced" class="advanced-search">
      <input v-model="filters.author" placeholder="作者" />
      <input v-model="filters.date" type="date" placeholder="日期" />
      <select v-model="filters.category">
        <option value="">所有分类</option>
        <option v-for="cat in categories" :value="cat.id">{{ cat.name }}</option>
      </select>
    </div>

    <button @click="handleSearch">搜索</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      showAdvanced: false,
      filters: {
        author: '',
        date: '',
        category: ''
      },
      categories: []
    }
  },
  created() {
    this.fetchCategories()
  },
  methods: {
    toggleAdvanced() {
      this.showAdvanced = !this.showAdvanced
    },
    async fetchCategories() {
      const response = await fetch('/api/categories')
      this.categories = await response.json()
    },
    handleSearch() {
      const searchParams = {
        q: this.searchQuery,
        ...this.filters
      }
      this.$emit('search', searchParams)
    }
  }
}
</script>

响应式设计优化

确保搜索组件在不同设备上都能良好显示:

.search-container {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

@media (min-width: 768px) {
  .search-container {
    flex-direction: row;
  }
}

input {
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
  width: 100%;
}

button {
  padding: 8px 16px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

这些实现方法涵盖了从基础到高级的搜索组件功能,可以根据具体需求进行选择和组合。

vue实现搜索组件

标签: 组件vue
分享给朋友:

相关文章

实现vue

实现vue

安装Vue.js 通过npm安装Vue.js是最常见的方式。确保已安装Node.js和npm,运行以下命令: npm install vue 或者使用CDN直接引入: <script s…

vue实现公式

vue实现公式

在Vue中实现公式展示或计算可以通过多种方式实现,以下为具体方法: 使用模板插值与计算属性 若公式较简单,可直接在模板中使用插值表达式或计算属性。例如计算圆的面积: <template&…

vue实现jqueryui

vue实现jqueryui

Vue 实现 jQuery UI 功能 在 Vue 项目中实现类似 jQuery UI 的功能,可以通过原生 Vue 组件或第三方库来实现。以下是几种常见 jQuery UI 功能的 Vue 替代方案…

vue实现$.extend

vue实现$.extend

Vue 实现类似 jQuery 的 $.extend 功能 在 Vue 中实现类似 jQuery 的 $.extend 功能,可以通过多种方式完成。$.extend 主要用于合并多个对象的属性,Vue…

vue滚动实现

vue滚动实现

Vue 滚动实现方法 使用原生滚动 在 Vue 中可以通过 ref 获取 DOM 元素,调用原生滚动方法实现滚动效果。适用于简单场景。 <template> <div ref=…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局引…