当前位置:首页 > VUE

vue实现热搜框

2026-01-23 04:31:53VUE

Vue 实现热搜框

基本结构

热搜框通常由输入框和下拉建议列表组成。使用 Vue 的 v-model 实现数据绑定,v-for 渲染建议列表。

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

数据与逻辑

定义搜索查询、建议列表和显示状态的响应式数据,通过输入事件触发建议更新。

<script>
export default {
  data() {
    return {
      searchQuery: '',
      suggestions: [],
      showSuggestions: false,
      hotKeywords: ['Vue3', 'React', 'JavaScript', 'TypeScript'] // 模拟热搜数据
    }
  },
  methods: {
    handleInput() {
      if (this.searchQuery) {
        // 模拟筛选逻辑,实际可替换为API请求
        this.suggestions = this.hotKeywords.filter(keyword => 
          keyword.toLowerCase().includes(this.searchQuery.toLowerCase())
        );
        this.showSuggestions = this.suggestions.length > 0;
      } else {
        this.showSuggestions = false;
      }
    },
    selectSuggestion(item) {
      this.searchQuery = item;
      this.showSuggestions = false;
      // 触发搜索操作
      this.$emit('search', item);
    }
  }
}
</script>

样式设计

为输入框和下拉列表添加基础样式,确保视觉层次清晰。

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

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

.suggestions {
  position: absolute;
  width: 100%;
  margin-top: 4px;
  padding: 0;
  list-style: none;
  background: white;
  border: 1px solid #eee;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

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

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

动态热搜数据

通过 API 获取实时热搜数据,使用 mounted 钩子初始化数据。

<script>
export default {
  // ...其他代码
  async mounted() {
    try {
      const response = await fetch('https://api.example.com/hot-keywords');
      this.hotKeywords = await response.json();
    } catch (error) {
      console.error('获取热搜失败:', error);
    }
  }
}
</script>

防抖优化

避免频繁触发搜索建议,使用 Lodash 的 debounce 函数。

import { debounce } from 'lodash';

export default {
  created() {
    this.debouncedHandleInput = debounce(this.handleInput, 300);
  },
  methods: {
    handleInput: debounce(function() {
      // 原有逻辑
    }, 300)
  }
}

键盘导航

支持键盘上下箭头选择建议项,增强用户体验。

methods: {
  handleKeyDown(event) {
    if (!this.showSuggestions) return;
    if (event.key === 'ArrowDown') {
      // 向下选择逻辑
    } else if (event.key === 'ArrowUp') {
      // 向上选择逻辑
    } else if (event.key === 'Enter') {
      // 确认选择
    }
  }
}

vue实现热搜框

标签: vue热搜框
分享给朋友:

相关文章

vue实现多个tab

vue实现多个tab

Vue 实现多个 Tab 的方法 使用动态组件切换 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现多个…

用vue实现搜索查询

用vue实现搜索查询

实现搜索查询的基本思路 在Vue中实现搜索查询功能通常涉及以下几个核心部分:数据绑定、事件监听、过滤逻辑和UI反馈。以下是一个完整的实现方案。 数据绑定与输入框 创建一个Vue组件,包含一个输入框用…

vue实现右下角弹框

vue实现右下角弹框

实现右下角弹框的基本思路 在Vue中实现右下角弹框,通常需要结合CSS定位和Vue的组件化特性。弹框可以通过绝对定位固定在右下角,并通过Vue控制其显示与隐藏。 创建弹框组件 新建一个Vue组件(如…

vue实现a4打印模板

vue实现a4打印模板

使用Vue实现A4打印模板 在Vue中实现A4打印模板需要结合CSS的打印样式和Vue的模板渲染能力。以下是具体实现方法: 设置A4纸张尺寸 在CSS中定义A4纸张的标准尺寸(210mm × 297…

vue 实现blog

vue 实现blog

Vue 实现博客的基本步骤 使用 Vue 实现博客可以分为前端和后端两部分,前端使用 Vue.js 框架,后端可以选择 Node.js、Python 或其他服务端语言。以下是一个基于 Vue 的博客实…

vue实现截图

vue实现截图

Vue 实现截图的方法 使用 html2canvas 库 安装 html2canvas 库: npm install html2canvas 在 Vue 组件中使用: import html2ca…