当前位置:首页 > VUE

vue实现搜索组件

2026-03-30 03:00:18VUE

Vue 实现搜索组件的方法

基础搜索组件实现

创建一个基础搜索组件,包含输入框和搜索按钮。使用 v-model 绑定输入值,通过 @click@keyup.enter 触发搜索。

<template>
  <div class="search-container">
    <input 
      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>

防抖优化

为避免频繁触发搜索请求,使用 Lodash 的 debounce 或手动实现防抖。

vue实现搜索组件

import { debounce } from 'lodash'

export default {
  methods: {
    handleSearch: debounce(function() {
      this.$emit('search', this.searchQuery)
    }, 500)
  }
}

异步搜索与加载状态

添加加载状态提示,适用于异步搜索场景。

<template>
  <div>
    <input v-model="searchQuery" @input="handleInput" />
    <span v-if="isLoading">搜索中...</span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLoading: false,
      searchQuery: ''
    }
  },
  methods: {
    async handleInput() {
      this.isLoading = true
      await this.$emit('search', this.searchQuery)
      this.isLoading = false
    }
  }
}
</script>

搜索建议功能

实现输入时动态显示搜索建议列表。

vue实现搜索组件

<template>
  <div class="search-suggestions">
    <input v-model="searchQuery" @input="fetchSuggestions" />
    <ul v-if="suggestions.length">
      <li 
        v-for="(item, index) in suggestions" 
        :key="index"
        @click="selectSuggestion(item)"
      >
        {{ item }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      suggestions: []
    }
  },
  methods: {
    fetchSuggestions() {
      // 调用API获取建议数据
      this.suggestions = ['Vue', 'React', 'Angular'].filter(item => 
        item.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    },
    selectSuggestion(item) {
      this.searchQuery = item
      this.suggestions = []
      this.$emit('search', item)
    }
  }
}
</script>

样式优化

添加基础样式增强用户体验。

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

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

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

.search-suggestions ul {
  list-style: none;
  padding: 0;
  border: 1px solid #eee;
}

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

.search-suggestions li:hover {
  background-color: #f5f5f5;
}

与父组件通信

父组件通过监听事件获取搜索值。

<template>
  <div>
    <SearchComponent @search="onSearch" />
    <div v-if="results">{{ results }}</div>
  </div>
</template>

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

export default {
  components: { SearchComponent },
  data() {
    return {
      results: ''
    }
  },
  methods: {
    onSearch(query) {
      this.results = `正在搜索: ${query}`
      // 实际调用API逻辑
    }
  }
}
</script>

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

相关文章

vue实现handsontable

vue实现handsontable

Vue 中实现 Handsontable 要在 Vue 项目中集成 Handsontable,可以使用官方提供的 @handsontable/vue 封装库。以下是一个完整的实现步骤。 安装依赖 确…

computed vue 实现

computed vue 实现

computed 在 Vue 中的基本用法 Vue 的 computed 属性用于声明依赖于其他数据的计算属性。当依赖的数据变化时,计算属性会自动重新计算并缓存结果。 export defaul…

vue实现换行

vue实现换行

Vue 中实现换行的几种方法 在 Vue 中实现换行可以通过多种方式,具体取决于使用场景和需求。以下是常见的几种方法: 使用 HTML 的 <br> 标签 在模板中直接使用 <br…

vue实现模块

vue实现模块

Vue 实现模块化的方法 Vue 支持多种模块化开发方式,可以根据项目需求选择适合的方案。 使用单文件组件(SFC) 单文件组件是 Vue 最常用的模块化方式,将模板、脚本和样式封装在一个 .vue…

vue实现动画

vue实现动画

Vue 实现动画的方法 Vue 提供了多种方式来实现动画效果,包括内置的过渡系统、第三方动画库以及直接操作 CSS 或 JavaScript 动画。 使用 Vue 内置过渡系统 Vue 的 <…

vue实现tree

vue实现tree

Vue 实现 Tree 组件 使用 Vue 实现 Tree 组件可以通过递归组件的方式来实现层级结构展示。以下是一个完整的实现方法: 基础递归组件实现 创建 Tree 组件文件 Tree.vue,使…