当前位置:首页 > VUE

vue实现商城搜索

2026-03-08 12:19:39VUE

Vue 实现商城搜索功能

搜索框组件设计

使用 Vue 的 v-model 实现双向数据绑定,监听用户输入。建议添加防抖处理(如 Lodash 的 _.debounce)避免频繁触发搜索。

<template>
  <div class="search-box">
    <input 
      v-model="keyword" 
      @keyup.enter="search" 
      placeholder="输入商品名称/品牌"
    />
    <button @click="search">搜索</button>
  </div>
</template>

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

搜索逻辑实现

通过 Vuex 或组件通信管理搜索状态,建议将搜索关键词作为路由参数实现可分享的 URL。

vue实现商城搜索

// 在页面组件中
methods: {
  handleSearch(keyword) {
    if (!keyword) return

    // 方式1:通过路由更新(推荐)
    this.$router.push({
      path: '/products',
      query: { q: keyword }
    })

    // 方式2:直接调用API
    this.fetchProducts({ keyword })
  },
  fetchProducts(params) {
    api.getProducts(params).then(res => {
      this.products = res.data.list
    })
  }
}

搜索结果展示

使用 v-for 渲染商品列表,建议添加加载状态和空状态提示。

vue实现商城搜索

<template>
  <div class="product-list">
    <div v-if="loading" class="loading">加载中...</div>
    <div v-else-if="!products.length" class="empty">暂无商品</div>
    <div v-else class="grid">
      <product-card 
        v-for="item in products" 
        :key="item.id" 
        :data="item"
      />
    </div>
  </div>
</template>

搜索建议功能

实现输入时的自动补全建议,需要与后端配合返回建议关键词。

watch: {
  keyword(newVal) {
    if (newVal) {
      this.fetchSuggestions(newVal)
    }
  }
},
methods: {
  fetchSuggestions: _.debounce(function(keyword) {
    api.getSuggestions(keyword).then(res => {
      this.suggestions = res.data
    })
  }, 300)
}

高级搜索筛选

添加价格区间、分类等筛选条件,使用对象管理筛选参数。

<div class="filters">
  <select v-model="filters.category">
    <option value="">全部分类</option>
    <option v-for="cat in categories" :value="cat.id">{{ cat.name }}</option>
  </select>
  <input v-model.number="filters.minPrice" placeholder="最低价">
  <input v-model.number="filters.maxPrice" placeholder="最高价">
</div>

性能优化建议

  1. 使用虚拟滚动(如 vue-virtual-scroller)处理大量商品列表
  2. 实现前端缓存已搜索过的结果
  3. 对图片使用懒加载(如 v-lazy)
  4. 分页加载(无限滚动或页码切换)
// 分页示例
data() {
  return {
    page: 1,
    pageSize: 20,
    hasMore: true
  }
},
methods: {
  loadMore() {
    if (!this.hasMore) return
    this.page++
    this.fetchProducts()
  },
  fetchProducts() {
    api.getProducts({
      keyword: this.keyword,
      page: this.page,
      pageSize: this.pageSize
    }).then(res => {
      this.products.push(...res.data.list)
      this.hasMore = res.data.hasMore
    })
  }
}

标签: 商城vue
分享给朋友:

相关文章

vue无限菜单怎么实现

vue无限菜单怎么实现

实现 Vue 无限菜单的方法 递归组件实现嵌套菜单 使用 Vue 的递归组件特性可以轻松实现无限级嵌套菜单。通过组件调用自身的方式处理嵌套数据。 <template> <ul&…

vue实现过程

vue实现过程

Vue 的实现过程 Vue 的实现过程可以分为多个核心模块,包括响应式系统、虚拟 DOM、模板编译等。以下是 Vue 的主要实现过程分析。 响应式系统 Vue 的响应式系统基于 Object.def…

vue SSG实现

vue SSG实现

Vue SSG 实现方法 Vue 的静态站点生成(SSG)可以通过多种工具和框架实现,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态站点生成器,适合文档…

vue实现翻译

vue实现翻译

Vue 实现翻译功能的方法 使用 i18n 插件实现多语言切换 安装 vue-i18n 插件: npm install vue-i18n 创建语言资源文件(如 en.json 和 zh.json):…

vue实现模块

vue实现模块

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

vue实现单据

vue实现单据

Vue 实现单据功能的方法 使用 Vue 实现单据功能通常涉及表单设计、数据绑定、验证和提交等环节。以下是具体实现方案: 表单设计与数据绑定 使用 Vue 的 v-model 指令实现表单数据的双向…