Sku — 상품 규격(SKU) 선택
쇼핑 앱 상세 페이지에서 색상·용량을 고르고 수량을 정한 뒤 구매 버튼으로 이어지는 상품 선택 패널이에요. 규격 선택, 수량 조절, 하단 구매 버튼 영역이 하나의 컴포넌트로 묶여 있어서 상품 선택 시트를 바로 구성할 수 있어요.
본문
상품 선택 화면에 자주 쓰이는 컴포넌트예요.
설치
import { createApp } from 'vue'
import { Sku } from '@nutui/nutui'
const app = createApp()
app.use(Sku)
기본 사용법
:::demo
<template>
<nut-cell :title="Basic Usage" desc="" @click="base = true"></nut-cell>
<nut-sku
v-model:visible="base"
:sku="data.sku"
:goods="data.goods"
@selectSku="selectSku"
@clickBtnOperate="clickBtnOperate"
@close="close"
></nut-sku>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue'
const base = ref(false)
const data = reactive({
sku: [],
goods: {}
})
onMounted(() => {
fetch('https://storage.360buyimg.com/nutui/3x/data.js')
.then((response) => response.json())
.then((res) => {
const { Sku, Goods, imagePathMap } = res
data.sku = Sku
data.goods = Goods
}) //执行结果是 resolve就调用then方法
.catch((err) => console.log('Oh, error', err)) //执行结果是 reject就调用catch方法
})
// 切换规格类目
const selectSku = (ss) => {
const { sku, skuIndex, parentSku, parentIndex } = ss
if (sku.disable) return false
data.sku[parentIndex].list.forEach((s) => {
s.active = s.id == sku.id
})
data.goods = {
skuId: sku.id,
price: '4599.00',
imagePath: '//img14.360buyimg.com/n4/jfs/t1/215845/12/3788/221990/618a5c4dEc71cb4c7/7bd6eb8d17830991.jpg'
}
}
// 底部操作按钮触发
const clickBtnOperate = (op) => {
console.log('点击了操作按钮', op)
}
// 关闭商品规格弹框
const close = () => {}
</script>
:::
판매 불가 케이스 (Not Sell)
:::demo
<template>
<nut-cell title="Not Sell" desc="" @click="notSell = true"></nut-cell>
<nut-sku
v-model:visible="notSell"
:sku="data.sku"
:goods="data.goods"
:btnExtraText="btnExtraText"
@changeStepper="changeStepper"
@selectSku="selectSku"
:btnOptions="['buy', 'cart']"
>
<template #sku-operate>
<div class="sku-operate-box">
<nut-button class="sku-operate-box-dis" type="warning">查看相似商品</nut-button>
<nut-button class="sku-operate-box-dis" type="info">到货通知</nut-button>
</div>
</template>
</nut-sku>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue'
const notSell = ref(false)
const data = reactive({
sku: [],
goods: {}
})
const btnExtraText = ref('抱歉,此商品在所选区域暂无存货')
onMounted(() => {
fetch('https://storage.360buyimg.com/nutui/3x/data.js')
.then((response) => response.json())
.then((res) => {
const { Sku, Goods, imagePathMap } = res
data.sku = Sku
data.goods = Goods
}) //执行结果是 resolve就调用then方法
.catch((err) => console.log('Oh, error', err)) //执行结果是 reject就调用catch方法
})
// inputNumber 更改
const changeStepper = (count) => {
console.log('购买数量', count)
}
// 切换规格类目
const selectSku = (ss) => {
const { sku, skuIndex, parentSku, parentIndex } = ss
if (sku.disable) return false
data.sku[parentIndex].list.forEach((s) => {
s.active = s.id == sku.id
})
data.goods = {
skuId: sku.id,
price: '4599.00',
imagePath: '//img14.360buyimg.com/n4/jfs/t1/216079/14/3895/201095/618a5c0cEe0b9e2ba/cf5b98fb6128a09e.jpg'
}
}
// 底部操作按钮触发
const clickBtnOperate = (op) => {
console.log('点击了操作按钮', op)
}
</script>
<style>
.sku-operate-box {
width: 100%;
display: flex;
padding: 8px 10px;
box-sizing: border-box;
}
.sku-operate-box-dis {
flex: 1;
}
.sku-operate-box-dis:first-child {
margin-right: 18px;
}
</style>
:::
수량(스테퍼) 커스터마이즈
숫자 입력 칸의 최댓값과 최솟값을 필요에 맞게 설정할 수 있어요.
:::demo
<template>
<nut-cell title="Custom Stepper" desc="" @click="customStepper = true"></nut-cell>
<nut-sku
v-model:visible="customStepper"
:sku="data.sku"
:goods="data.goods"
:stepperMax="7"
:stepperMin="2"
:stepperExtraText="stepperExtraText"
@changeStepper="changeStepper"
@overLimit="overLimit"
:btnOptions="['buy', 'cart']"
@selectSku="selectSku"
@clickBtnOperate="clickBtnOperate"
></nut-sku>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue'
import { showToast } from '@nutui/nutui'
const customStepper = ref(false)
const data = reactive({
sku: [],
goods: {}
})
onMounted(() => {
fetch('https://storage.360buyimg.com/nutui/3x/data.js')
.then((response) => response.json())
.then((res) => {
const { Sku, Goods, imagePathMap } = res
data.sku = Sku
data.goods = Goods
}) //执行结果是 resolve就调用then方法
.catch((err) => console.log('Oh, error', err)) //执行结果是 reject就调用catch方法
})
const stepperExtraText = () => {
return `<div style="width:100%;text-align:right;color:#F00">2 件起售</div>`
}
// inputNumber 更改
const changeStepper = (count) => {
console.log('购买数量', count)
}
// inputNumber 极限值
const overLimit = (val) => {
if (val.action == 'reduce') {
showToast.text(`至少买${val.value}件哦`)
} else {
showToast.text(`最多买${val.value}件哦`)
}
}
// 切换规格类目
const selectSku = (ss) => {
const { sku, skuIndex, parentSku, parentIndex } = ss
if (sku.disable) return false
data.sku[parentIndex].list.forEach((s) => {
s.active = s.id == sku.id
})
data.goods = {
skuId: sku.id,
price: '4599.00',
imagePath: '//img14.360buyimg.com/n4/jfs/t1/215845/12/3788/221990/618a5c4dEc71cb4c7/7bd6eb8d17830991.jpg'
}
}
// 底部操作按钮触发
const clickBtnOperate = (op) => {
console.log('点击了操作按钮', op)
}
</script>
:::
슬롯 커스터마이즈
기본 레이아웃은 여러 영역으로 나뉘어 있고, 각 영역이 슬롯으로 정의되어 있어서 필요에 따라 원하는 내용으로 바꿀 수 있어요.
:::demo
<template>
<nut-cell title="Custom Slots" desc="" @click="customBySlot = true"></nut-cell>
<nut-sku
v-model:visible="customBySlot"
:sku="data.sku"
:goods="data.goods"
:btnOptions="['buy', 'cart']"
@selectSku="selectSku"
@clickBtnOperate="clickBtnOperate"
>
<!-- 商品展示区,价格区域 -->
<template #sku-header-price>
<div>
<nut-price :price="goods.price" :needSymbol="true" :thousands="false"> </nut-price>
<span class="tag"></span>
</div>
</template>
<!-- 商品展示区,编号区域 -->
<template #sku-header-extra>
<span class="nut-sku-header-right-extra">重量:0.1kg 编号:{{ skuId }} </span>
</template>
<!-- sku 展示区上方与商品信息展示区下方区域,无默认展示内容 -->
<template #sku-select-top>
<div class="address">
<nut-cell
style="box-shadow:none;padding:13px 0"
title="送至"
:desc="addressDesc"
@click="showAddressPopup = true"
></nut-cell>
</div>
</template>
<!-- 底部按钮操作区 -->
<template #sku-operate>
<div class="sku-operate-box">
<nut-button class="sku-operate-item" shape="square" type="warning">加入购物车</nut-button>
<nut-button class="sku-operate-item" shape="square" type="primary">立即购买</nut-button>
</div>
</template>
</nut-sku>
<nut-address
v-model:visible="showAddressPopup"
type="exist"
:exist-address="existAddress"
:is-show-custom-address="false"
@selected="selectedAddress"
exist-address-title="配送至"
></nut-address>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue'
const customBySlot = ref(false)
const showAddressPopup = ref(false)
const data = reactive({
sku: [],
goods: {}
})
const addressDesc = ref('(配送地会影响库存,请先确认)')
const existAddress = ref([
{
id: 1,
addressDetail: 'th ',
cityName: '石景山区',
countyName: '城区',
provinceName: '北京',
selectedAddress: true,
townName: ''
},
{
id: 2,
addressDetail: '12 ',
cityName: '电饭锅',
countyName: '扶绥县',
provinceName: '北京',
selectedAddress: false,
townName: ''
},
{
id: 3,
addressDetail: '发大水比 ',
cityName: '放到',
countyName: '广宁街道',
provinceName: '钓鱼岛全区',
selectedAddress: false,
townName: ''
},
{
id: 4,
addressDetail: '还是想吧百度吧 ',
cityName: '研发',
countyName: '八里庄街道',
provinceName: '北京',
selectedAddress: false,
townName: ''
}
])
onMounted(() => {
fetch('https://storage.360buyimg.com/nutui/3x/data.js')
.then((response) => response.json())
.then((res) => {
const { Sku, Goods, imagePathMap } = res
data.sku = Sku
data.goods = Goods
}) //执行结果是 resolve就调用then方法
.catch((err) => console.log('Oh, error', err)) //执行结果是 reject就调用catch方法
})
// 切换规格类目
const selectSku = (ss) => {
const { sku, skuIndex, parentSku, parentIndex } = ss
if (sku.disable) return false
data.sku[parentIndex].list.forEach((s) => {
s.active = s.id == sku.id
})
data.goods = {
skuId: sku.id,
price: '6002.10',
imagePath: '//img14.360buyimg.com/n4/jfs/t1/215845/12/3788/221990/618a5c4dEc71cb4c7/7bd6eb8d17830991.jpg'
}
}
const selectedAddress = (prevExistAdd, nowExistAdd) => {
const { provinceName, countyName, cityName } = nowExistAdd
addressDesc.value = `${provinceName}${countyName}${cityName}`
}
// 底部操作按钮触发
const clickBtnOperate = (op) => {
console.log('点击了操作按钮', op)
}
</script>
<style>
.sku-operate-box {
width: 100%;
display: flex;
padding: 8px 10px;
box-sizing: border-box;
}
.sku-operate-item {
flex: 1;
}
.sku-operate-item:first-child {
border-top-left-radius: 20px;
border-bottom-left-radius: 20px;
}
.sku-operate-item:last-child {
border-top-right-radius: 20px;
border-bottom-right-radius: 20px;
}
</style>
:::
API
Props
| Attribute | Description | Type | Default |
|---|---|---|---|
| v-model:visible | Whether to open popup | boolean | false |
| sku | Sku data | Array | [] |
| goods | Product Info | object | - |
| stepper-max | Stepper max | string | number | 99999 |
| stepper-min | Stepper min | string | number | 1 |
| btn-options | Bottom button | Array | [confirm] |
| btn-extra-text | Add text above button | string | - |
| stepper-title | Stepper left text | string | Buy Num |
| stepper-extra-text | The text between the stepper and the headline | Function | boolean | false |
| buy-text | Buy button text | string | Buy It Now |
| add-cart-text | Add cart button text | string | Add To cart |
| confirm-text | Confirm button text | string | Confirm |
Events
| Attribute | Description | Arguments |
|---|---|---|
| select-sku | Emitted when select sku | {sku,skuIndex,parentSku,parentIndex} |
| add | Emitted when click stepper add button | value |
| reduce | Emitted when click stepper reduce button | value |
| overLimit | Emitted when click stepper disabled button | value |
| change-stepper | Emitted when click stepper change | value |
| click-btn-operate | Emitted when click bottom button | {type:'confirm',value:'inputNumber value'} |
| click-close-icon | Emitted when click close button | - |
| click-overlay | Emitted when click mask | - |
| close | Emitted when popup close | - |
Slots
기본 레이아웃의 여러 영역이 슬롯으로 정의되어 있어서, 필요에 따라 원하는 내용으로 교체할 수 있어요.
| Event | Description |
|---|---|
| sku-header | Custom header |
| sku-header-price | Custom header price area |
| sku-header-extra | Extra header area |
| sku-select-top | Custom select top |
| sku-select | Custom sku |
| sku-stepper | Custom stepper |
| sku-stepper-bottom | Custom stepper bottom |
| sku-operate | Custom stepper bottom operation |
goods 데이터 구조
goods:{
skuId:'', // Product ID
price: "0", // Product Price
imagePath: "", // Product Image
}
sku 데이터 구조
배열의 각 인덱스는 하나의 규격 분류를 나타내요. 각 분류의 list에는 그 분류의 선택지 값들이 들어가고, 선택지 객체는 name, id, active, disable 값을 가져요.
sku: [
{
id: 1,
name: '颜色',
list: [
{
name: '亮黑色',
id: 100016015112,
active: true,
disable: false
},
{
name: '釉白色',
id: 100016015142,
active: false,
disable: false
},
{
name: '秘银色',
id: 100016015078,
active: false,
disable: false
},
{
name: '夏日胡杨',
id: 100009064831,
active: false,
disable: false
},
{
name: '秋日胡杨',
id: 100009064830,
active: false,
disable: false
}
]
},
{
id: 2,
name: '版本',
list: [
{
name: '8GB+128GB',
id: 100016015102,
active: true,
disable: false
},
{
name: '8GB+256GB',
id: 100016015122,
active: false,
disable: false
}
]
},
{
id: 3,
name: '版本',
list: [
{
name: '4G(有充版)',
id: 100016015103,
active: true,
disable: false
},
{
name: '5G(有充版)',
id: 100016015123,
active: false,
disable: false
},
{
name: '5G(无充版)',
id: 100016015104,
active: true,
disable: true
},
{
name: '5G(无充)质保换新版',
id: 100016015125,
active: false,
disable: false
}
]
}
]
테마 (Theming)
CSS 변수
이 컴포넌트는 스타일을 커스터마이징할 때 쓸 수 있는 CSS 변수를 제공해요. 자세한 내용은 ConfigProvider 컴포넌트 문서를 참고하세요.
| Name | Default Value |
|---|---|
| --nut-sku-item-border | 1px solid var(--nut-primary-color) |
| --nut-sku-item-disable-line | line-through |
| --nut-sku-opetate-bg-default | linear-gradient(90deg, var(--nut-primary-color), var(--nut-primary-color-end) 100%) |
| --nut-sku-item-active-bg | var(--nut-primary-color) |
| --nut-sku-opetate-bg-buy | linear-gradient(135deg,rgba(255, 186, 13, 1) 0%,rgba(255, 195, 13, 1) 69%,rgba(255, 207, 13, 1) 100%) |
| --nut-sku-spec-height | 30px |
| --nut-sku-spec-line-height | var(--nut-sku-spec-height) |
| --nut-sku-spec-font-size | 11px |
| --nut-sku-spec-background | rgba(242, 242, 242, 1) |
| --nut-sku-spec-color | var(--nut-black) |
| --nut-sku-spec-margin-right | 12px |
| --nut-sku-spec-padding | 0 18px |
| --nut-sku-spec-title-font-weight | bold |
| --nut-sku-spec-title-font-size | 13px |
| --nut-sku-spec-title-color | var(--nut-black) |
| --nut-sku-spec-title-margin-bottom | 18px |
| --nut-sku-operate-btn-height | 54px |
| --nut-sku-operate-btn-border-top | 0 |
| --nut-sku-operate-btn-item-height | 40px |
| --nut-sku-operate-btn-item-line-height | var(--nut-sku-operate-btn-item-height) |
| --nut-sku-operate-btn-item-font-size | 15px |
| --nut-sku-operate-btn-item-font-weight | normal |
| --nut-sku-product-img-width | 100px |
| --nut-sku-product-img-height | var(--nut-sku-product-img-width) |
| --nut-sku-product-img-border-radius | 0 |
더 알아보기 (Learn more)
이 문서는 NutUI v4 공식 영문 문서를 한국어로 옮긴 글이에요. 최신 API와 변경 사항은 원문에서 확인하는 게 가장 정확해요.
- 원문 문서: Sku · doc.en-US.md
- NutUI 공식 문서: nutui.jd.com / GitHub 저장소: jd-opensource/nutui