当前位置:首页 > VUE

vue sku 实现

2026-01-13 08:37:35VUE

Vue SKU 实现方法

在电商项目中,SKU(Stock Keeping Unit)是商品属性的组合选择功能。以下是基于 Vue 的实现方案:

数据结构设计

需要定义商品属性和 SKU 列表的数据结构:

data() {
  return {
    goods: {
      specs: [
        { name: '颜色', values: ['红色', '蓝色'] },
        { name: '尺寸', values: ['S', 'M'] }
      ],
      skus: [
        { specs: ['红色', 'S'], price: 100, stock: 10 },
        { specs: ['红色', 'M'], price: 110, stock: 5 }
      ]
    },
    selected: {}
  }
}

选择逻辑实现

通过计算属性处理已选规格和可用规格:

vue sku 实现

computed: {
  selectedSpecs() {
    return Object.values(this.selected)
  },

  availableSpecs() {
    const available = {}
    this.goods.skus.forEach(sku => {
      if (this.isSkuAvailable(sku.specs)) {
        sku.specs.forEach((spec, index) => {
          if (!available[index]) available[index] = new Set()
          available[index].add(spec)
        })
      }
    })
    return available
  }
},

methods: {
  isSkuAvailable(specs) {
    return this.selectedSpecs.every(
      (selected, index) => !selected || specs[index] === selected
    )
  }
}

模板渲染

使用 v-for 渲染规格选择界面:

<div v-for="(spec, index) in goods.specs" :key="index">
  <h3>{{ spec.name }}</h3>
  <button 
    v-for="value in spec.values" 
    :key="value"
    :disabled="!isSpecAvailable(index, value)"
    @click="selectSpec(index, value)"
    :class="{ active: selected[index] === value }"
  >
    {{ value }}
  </button>
</div>

状态更新方法

处理规格选择逻辑:

vue sku 实现

methods: {
  selectSpec(index, value) {
    if (this.selected[index] === value) {
      this.$delete(this.selected, index)
    } else {
      this.$set(this.selected, index, value)
    }
  },

  isSpecAvailable(index, value) {
    const tempSelected = { ...this.selected, [index]: value }
    return this.goods.skus.some(sku => {
      return sku.specs.every(
        (spec, i) => !tempSelected[i] || spec === tempSelected[i]
      )
    })
  }
}

完整 SKU 匹配

当所有规格选中时,显示对应 SKU 信息:

computed: {
  currentSku() {
    if (Object.keys(this.selected).length !== this.goods.specs.length) {
      return null
    }
    return this.goods.skus.find(sku => 
      sku.specs.every((spec, index) => spec === this.selected[index])
    )
  }
}

界面优化技巧

  1. 禁用不可选规格样式
    
    button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    }

button.active { border-color: red; }


2. 添加库存提示
```html
<div v-if="currentSku">
  价格:{{ currentSku.price }} 库存:{{ currentSku.stock }}
</div>
<div v-else>
  请选择完整规格
</div>

通过以上方法可以实现基本的 Vue SKU 选择功能。实际项目中可能需要根据业务需求调整数据结构或交互逻辑。

标签: vuesku
分享给朋友:

相关文章

vue实现下拉菜单

vue实现下拉菜单

实现下拉菜单的基本思路 使用Vue实现下拉菜单可以通过动态绑定v-show或v-if控制菜单显示隐藏,结合事件监听(如@click或@mouseenter)触发状态切换。以下是两种常见实现方式: 方…

vue实现检测数组

vue实现检测数组

Vue 中检测数组变化的方法 Vue 的响应式系统无法直接检测到数组的某些变化,因为 JavaScript 的限制。以下是一些解决方案: 使用 Vue.set 或 this.$set Vue…

vue前端实现搜索

vue前端实现搜索

实现搜索功能的基本方法 在Vue中实现搜索功能通常涉及以下几个关键步骤,结合数据绑定、计算属性和方法调用来动态过滤和显示结果。 数据绑定与输入处理 使用v-model双向绑定搜索输入框的值,监听用户…

vue怎么实现页面返回

vue怎么实现页面返回

Vue 实现页面返回的方法 在 Vue 中实现页面返回功能,可以通过以下几种方式完成,具体取决于项目使用的路由模式和技术栈。 使用 Vue Router 的编程式导航 通过 this.$router…

vue实现适老化样式

vue实现适老化样式

Vue 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { --…

vue弹幕实现不重叠

vue弹幕实现不重叠

实现 Vue 弹幕不重叠的方法 CSS 定位与动画控制 通过绝对定位和动态计算弹幕轨道高度,确保弹幕在不同轨道上运行。每条弹幕占据固定高度的轨道,避免重叠。 // 弹幕轨道高度计算 const t…