当前位置:首页 > VUE

vue实现search组件

2026-01-16 21:52:16VUE

Vue 实现搜索组件

基础实现

创建一个基础的搜索组件需要包含输入框和搜索逻辑。以下是一个简单的实现示例:

<template>
  <div class="search-container">
    <input 
      v-model="searchQuery" 
      @input="handleSearch" 
      placeholder="Search..."
    />
    <ul v-if="filteredItems.length">
      <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())
      )
    }
  }
}
</script>

使用计算属性优化

计算属性可以自动响应依赖变化,避免手动触发搜索:

vue实现search组件

<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>

添加防抖功能

频繁触发搜索会影响性能,可以使用防抖函数优化:

<script>
import { debounce } from 'lodash'

export default {
  data() {
    return {
      searchQuery: '',
      filteredItems: []
    }
  },
  created() {
    this.debouncedSearch = debounce(this.doSearch, 300)
  },
  methods: {
    handleSearch() {
      this.debouncedSearch()
    },
    doSearch() {
      // 实际搜索逻辑
      this.filteredItems = this.items.filter(item =>
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

支持异步搜索

当需要从API获取搜索结果时:

vue实现search组件

<script>
export default {
  methods: {
    async handleSearch() {
      if (!this.searchQuery.trim()) return

      try {
        const response = await axios.get('/api/search', {
          params: { q: this.searchQuery }
        })
        this.searchResults = response.data
      } catch (error) {
        console.error('Search failed:', error)
      }
    }
  }
}
</script>

样式优化

为搜索组件添加基础样式:

.search-container {
  position: relative;
}

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

ul {
  position: absolute;
  width: 100%;
  max-height: 200px;
  overflow-y: auto;
  background: white;
  border: 1px solid #eee;
  margin-top: 4px;
  padding: 0;
  list-style: none;
}

li {
  padding: 8px 12px;
  cursor: pointer;
}

li:hover {
  background-color: #f5f5f5;
}

组件化封装

将搜索功能封装为可复用组件:

<!-- SearchComponent.vue -->
<template>
  <div class="search-component">
    <slot name="input" :search="search">
      <input
        v-model="searchQuery"
        @input="handleSearch"
        :placeholder="placeholder"
      />
    </slot>

    <slot name="results" :results="filteredItems">
      <ul v-if="showResults && filteredItems.length">
        <li 
          v-for="item in filteredItems" 
          :key="getKey(item)"
          @click="selectItem(item)"
        >
          {{ getItemText(item) }}
        </li>
      </ul>
    </slot>
  </div>
</template>

<script>
export default {
  props: {
    items: {
      type: Array,
      required: true
    },
    placeholder: {
      type: String,
      default: 'Search...'
    },
    filterFn: {
      type: Function,
      default: (item, query) => 
        item.text.toLowerCase().includes(query.toLowerCase())
    },
    getKey: {
      type: Function,
      default: item => item.id
    },
    getItemText: {
      type: Function,
      default: item => item.text
    }
  },
  data() {
    return {
      searchQuery: '',
      showResults: false
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item =>
        this.filterFn(item, this.searchQuery)
      )
    }
  },
  methods: {
    handleSearch() {
      this.showResults = true
    },
    selectItem(item) {
      this.$emit('select', item)
      this.showResults = false
      this.searchQuery = this.getItemText(item)
    }
  }
}
</script>

使用示例

<template>
  <SearchComponent 
    :items="products" 
    @select="handleSelect"
    placeholder="Search products..."
  />
</template>

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

export default {
  components: { SearchComponent },
  data() {
    return {
      products: [
        { id: 1, name: 'Laptop', price: 999 },
        { id: 2, name: 'Phone', price: 699 }
      ]
    }
  },
  methods: {
    handleSelect(product) {
      console.log('Selected:', product)
    }
  }
}
</script>

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

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

相关文章

vue 路由实现

vue 路由实现

Vue 路由实现方法 安装 Vue Router 使用 npm 或 yarn 安装 Vue Router 依赖包: npm install vue-router # 或 yarn add vue-…

vue实现图片搜索

vue实现图片搜索

Vue实现图片搜索功能 使用HTML5的File API获取图片 通过<input type="file">元素让用户选择图片文件,使用FileReader对象读取图片数据。 <t…

vue实现皮肤切换

vue实现皮肤切换

实现皮肤切换的基本思路 在Vue中实现皮肤切换功能,通常涉及动态修改CSS变量或类名。核心是通过状态管理当前主题,并在组件中应用对应的样式。 使用CSS变量实现主题切换 CSS变量(自定义属性)是…

前端vue登录功能实现

前端vue登录功能实现

Vue 登录功能实现步骤 创建登录表单组件 在 Vue 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含用户名和密码输入框以及提交按钮。 <template> &l…

vue 实现简单登陆

vue 实现简单登陆

以下是一个使用 Vue 3 实现的简单登录功能示例,包含表单验证和基础交互逻辑: 创建 Vue 组件 <template> <div class="login-containe…

vue实现倒计时组件

vue实现倒计时组件

实现基础倒计时功能 使用 Vue 的 data 和 methods 定义倒计时逻辑,通过 setInterval 更新剩余时间。 <template> <div>{{…