Skip to content

Subsection 分段器 1.1.4

手机扫码预览
17:42

组件名:rice-subsection

用于切换不同的区域内容。注:1.1.4版本开始支持

平台兼容性

uni-app x

AndroidiOSiOS(Vapor)HarmonyOSHarmonyOS(Vapor)微信小程序h5
√(1.1.3+)√(1.1.0+)

基础用法

通过 v-model/model-value 可以控制选中的下标

vue
<template>
  <rice-subsection :list="list1" v-model="current" />
</template>

<script setup lang="ts">
  const list1 = ref<string[]>(['香蕉', '苹果', '大西瓜'])
  const current = ref(0)

</script>

胶囊形状

shape设置为 round 即可

vue
<rice-subsection :list="list1" v-model="current"  shape="round" />

异步控制

before-change 函数 返回 false 或者返回 Promise 且被 rejectresolve 但返回值不为 true,则停止切换

vue
<template>
  <rice-subsection :list="list2" v-model="current2" :before-change="beforeChange" />
</template>

<script setup lang="ts">
	const list2 = ref<string[]>(['年', '月', '日', '每周'])
	const current2 = ref(1)
	const beforeChange = (index : number) => {
		return new Promise((resolve) => {
			if (index == 3) {
				uni.showToast({
					title: `${list2.value[index]} 不可选择`,
					icon: "none"
				})
				resolve(false)
			} else {
				uni.showLoading({
					title: '切换中…',
					mask: true
				})
				setTimeout(() => {
					resolve(true)
					uni.hideLoading()
				}, 500)
			}

		})
	}
</script>

插槽

插槽会返回 index 和 subname 参数

vue
<template>
  <rice-subsection :list="list1" :model-value="modelValue" @change="onChange">
    <template #default="{index,subname}">
      <view class="slot-item-class">
        <image v-if="index==0" class="slot-icon" src="/static/images/jb.png"></image>
        <image v-else class="slot-icon" src="/static/images/wm.png"></image>
        <text class="slot-text">{{subname}}</text>
      </view>
    </template>
  </rice-subsection>
</template>

自定义样式

自定义class (根节点的class除外) 在小程序中不能写在 scoped 里面 且需要加!important, 不然可能样式不生效,其他平台无此问题

vue
<template>
  <rice-subsection :list="list1" class="subsection" text-class="text-class" active-text-class="active-text-class"
    inactive-text-class="inactive-text-class" bar-class="bar-class" :duration="120"/>
</template>
<style>
.text-class {
  font-size: 13px  !important;
}
.active-text-class {
  color: #fff  !important;
  transform: scale(1.03)  !important;
  font-weight: bold;
}
.inactive-text-class {
  color: var(--app-text-color-2);
  transform: scale(1);
}
.bar-class {
  background-color: #1989fa !important;
}
</style>

API

Props

属性名类型说明默认值
listString[]选项-
shapeString形状 round/squaresquare
init-animateBoolean初始化时是否使用动画false
durationNumber动画时长,单位ms300
before-change(index:number)=>Promise<boolean>|boolean返回 false 或者返回 Promise 且被 rejectresolve 但返回值不为 true,则停止切换-
bar-classString激活滑块的class-
item-classString每个选项的class-
text-classString文字的class-
active-text-classString激活文字的class-
inactive-text-classString非激活文字的class-

Events

事件名说明回调参数
change切换时触发index
clickSub点击时触发index

Slots

名称说明参数
default自定义item{index:当前的索引,subname:当前的值}

方法

名称说明
init父元素尺寸发生改变时,可以调用此方法重新计算,一般用于小程序

类型定义

组件类型

ts
// 组件类型
const subsectionRef = ref<RiceSubsectionComponentPublicInstance | null>(null)