当前位置:首页 > 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: {}
  }
}

选择逻辑实现

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

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>

状态更新方法

处理规格选择逻辑:

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

vue sku 实现


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

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

标签: vuesku
分享给朋友:

相关文章

vue实现文字播放栏

vue实现文字播放栏

Vue 实现文字播放栏(跑马灯效果) 方法一:使用 CSS 动画 + Vue 数据绑定 通过 CSS 的 @keyframes 实现动画效果,结合 Vue 的动态数据绑定控制内容。 <tem…

vue实现水印

vue实现水印

Vue 实现水印的方法 使用 CSS 背景图 通过 CSS 的 background-image 和 background-repeat 属性实现水印效果。创建一个包含水印文本的 Canvas,将其转…

vue代理实现

vue代理实现

Vue 代理实现方法 在 Vue 项目中,配置代理可以解决开发环境下的跨域问题,通常通过修改 vue.config.js 文件实现。以下是几种常见的代理配置方式: 基础代理配置 在 vue.conf…

vue核心实现

vue核心实现

Vue 核心实现原理 Vue.js 的核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是关键实现原理的详细说明: 响应式系统 Vue 使用 Object.defineProper…

vue实现门户

vue实现门户

Vue 实现门户网站的关键步骤 搭建基础框架 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 管理多页面路由。门户通常需要响应式布局,可引入 UI 库如 Element P…

vue 实现单点登录

vue 实现单点登录

单点登录(SSO)实现原理 单点登录允许用户通过一次身份验证访问多个系统。核心原理是用户首次登录后,认证中心颁发令牌(如Token),其他系统通过验证令牌实现免登录。 Vue中实现SSO的方案 基于…