当前位置:首页 > VUE

vue实现商品sku

2026-01-18 07:02:32VUE

Vue 实现商品 SKU 选择功能

数据结构设计

商品 SKU 通常由多个属性组合而成,例如颜色、尺寸等。需要设计一个嵌套数据结构来存储这些信息。

data() {
  return {
    skuData: {
      colors: ['红色', '蓝色', '绿色'],
      sizes: ['S', 'M', 'L'],
      // 库存信息,key 为属性组合
      stock: {
        '红色-S': 10,
        '红色-M': 5,
        '蓝色-L': 8
        // 其他组合...
      }
    },
    selected: {
      color: '',
      size: ''
    }
  }
}

模板渲染

使用 v-for 循环渲染可选的 SKU 属性,并通过计算属性判断哪些选项可选。

<div class="sku-selector">
  <div class="attribute">
    <h3>颜色</h3>
    <button 
      v-for="color in skuData.colors" 
      :key="color"
      @click="selectAttribute('color', color)"
      :class="{ active: selected.color === color, disabled: !isAttributeAvailable('color', color) }"
    >
      {{ color }}
    </button>
  </div>

  <div class="attribute">
    <h3>尺寸</h3>
    <button 
      v-for="size in skuData.sizes" 
      :key="size"
      @click="selectAttribute('size', size)"
      :class="{ active: selected.size === size, disabled: !isAttributeAvailable('size', size) }"
    >
      {{ size }}
    </button>
  </div>
</div>

交互逻辑实现

实现属性选择和库存检查的方法。

methods: {
  selectAttribute(type, value) {
    this.selected[type] = value
    this.checkCombination()
  },

  isAttributeAvailable(type, value) {
    const tempSelected = { ...this.selected }
    tempSelected[type] = value
    const combination = this.getCombinationKey(tempSelected)
    return this.skuData.stock[combination] > 0
  },

  getCombinationKey(selected) {
    return `${selected.color}-${selected.size}`
  },

  checkCombination() {
    const combination = this.getCombinationKey(this.selected)
    if (this.skuData.stock[combination]) {
      console.log('当前选择有库存')
    } else {
      console.log('当前选择无库存')
    }
  }
}

样式处理

为选中的状态和不可选状态添加样式区分。

.sku-selector button {
  margin: 5px;
  padding: 8px 12px;
  border: 1px solid #ddd;
  background: #fff;
  cursor: pointer;
}

.sku-selector button.active {
  border-color: #ff6700;
  color: #ff6700;
}

.sku-selector button.disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

完整示例改进

对于更复杂的 SKU 情况,可以使用递归组件处理多级属性。

vue实现商品sku

// SKU 组件
Vue.component('sku-selector', {
  props: ['attributes', 'selected', 'stock'],
  template: `
    <div class="sku-attribute">
      <h3>{{ attributes.name }}</h3>
      <div class="sku-values">
        <button
          v-for="value in attributes.values"
          :key="value"
          @click="$emit('select', attributes.name, value)"
          :class="{
            active: selected[attributes.name] === value,
            disabled: !isAvailable(attributes.name, value)
          }"
        >
          {{ value }}
        </button>
      </div>
    </div>
  `,
  methods: {
    isAvailable(name, value) {
      const temp = { ...this.selected, [name]: value }
      return this.checkStock(temp)
    },
    checkStock(selected) {
      const key = Object.values(selected).join('-')
      return this.stock[key] > 0
    }
  }
})

注意事项

  • 后端 API 通常返回所有 SKU 组合及其库存信息
  • 对于大量 SKU 的情况,考虑性能优化,如虚拟滚动
  • 可以添加 SKU 图片切换功能,当选择不同颜色时显示对应商品图
  • 移动端适配需要考虑触摸反馈和布局调整

这种实现方式提供了良好的用户体验,清晰地展示了可选和不可选的 SKU 组合,帮助用户快速完成商品规格选择。

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

相关文章

vue实现grid

vue实现grid

Vue 实现 Grid 布局的方法 使用 CSS Grid 布局 Vue 可以结合 CSS Grid 布局实现灵活的网格系统。CSS Grid 是现代浏览器原生支持的布局方案,无需额外依赖库。 &l…

vue 实现后退

vue 实现后退

实现后退功能的方法 在Vue中实现后退功能通常需要结合浏览器历史记录API或Vue Router的导航方法。以下是几种常见的实现方式: 使用Vue Router的go方法 this.$router…

vue实现pc

vue实现pc

Vue 实现 PC 端应用开发 Vue.js 是一个流行的前端框架,适用于构建 PC 端 Web 应用。以下是关键步骤和最佳实践: 项目初始化 使用 Vue CLI 或 Vite 创建项目:…

vue实现ide

vue实现ide

Vue 实现 IDE 的基本思路 使用 Vue 构建一个简易的集成开发环境(IDE)需要整合代码编辑器、文件管理、终端模拟等功能。以下是关键实现步骤和推荐工具。 核心功能模块 代码编辑器 使用…

vue实现select

vue实现select

Vue 实现 Select 组件的方法 在 Vue 中实现 Select 组件可以通过多种方式完成,以下是几种常见的方法: 使用原生 HTML select 元素 原生 HTML 的 <sel…

node vue实现

node vue实现

Node.js 与 Vue.js 实现方案 环境搭建 安装 Node.js(建议 LTS 版本),通过 npm 或 yarn 初始化项目。Vue.js 可通过 Vue CLI 快速搭建: npm…