Skip to content

LimeSegmented 分段器组件

一个功能丰富的分段器组件,用于在不同视图间切换。支持多种样式、尺寸和自定义选项,可用于标签切换、分类筛选等多种场景。组件提供了丰富的自定义选项,可以满足各种复杂的界面设计需求。

插件依赖:lime-sharedlime-style

文档链接

📚 组件详细文档请访问以下站点:

安装方法

  1. 在uni-app插件市场入口 中搜索并导入lime-segmented
  2. 导入后可能需要重新编译项目
  3. 在页面中使用l-segmented组件

代码演示

基础用法

最简单的分段器组件用法,只需要设置选项数组即可。

html
<l-segmented v-model="value" :options="options" />
js
const value = ref(1);
const options = ['选项1','选项2','选项3','选项4'];

形状

通过shape设置分段器形状,可选值round

html
<l-segmented shape="round" />

类型

通过type设置分段器类型,可选值有buttontext,默认值为card

html
<l-segmented v-model="value" type="button" />
<l-segmented v-model="value" type="text" />

尺寸

通过size设置分段器尺寸,可选值有smalllarge,默认值为medium

html
<l-segmented v-model="value" size="small" />
<l-segmented v-model="value" size="large" />

自定义样式

通过color设置文本颜色,active-color设置当前状态文本颜色,bg-color设置背景色,slider-color设置滑块背景色。

html
<l-segmented 
  v-model="value" 
  type="button" 
  color="#34c471" 
  active-color="#fff" 
  bg-color="#f3fff8" 
  slider-color="#34c471" 
  :options="options" 
/>

受控模式

通过设置value属性和click事件实现异步控制。

html
<l-segmented :value="value" :options="options" @click="handleClick" />
js
const value = ref(1);
const handleClick = (index: number) => {
  uni.showLoading({ title: '校验中' });
  
  setTimeout(() => {
    uni.hideLoading();
    value.value = index;
  }, 1000);
};

快速预览

导入插件后,可以直接使用以下标签查看演示效果:

html
<!-- 代码位于 uni_modules/lime-segmented/components/lime-segmented -->
<lime-segmented />

插件标签说明

  • 默认 l-segmented 为组件标签
  • 默认 lime-segmented 为演示标签

Vue2使用说明

本插件使用了composition-api,如需在Vue2项目中使用,请按照官方教程配置。

关键配置代码(在main.js中添加):

js
// vue2
import Vue from 'vue'
import VueCompositionAPI from '@vue/composition-api'
Vue.use(VueCompositionAPI)

API文档

Props

属性名说明类型默认值
v-model当前选中分段的索引值number-
value当前选中分段的索引值number-
size分段器尺寸,可选值smallmediumlargestringmedium
shape分段器形状,可选值normalroundstringnormal
disabled是否禁用分段器booleanfalse
type分段器类型,可选值buttoncardtextstringcard
bgColor分段器背景色string-
color分段器文本颜色string-
activeColor当前选中分段的文本颜色string-
sliderColor分段器滑块背景色string-
fontSize分段器文字大小string-
height分段器高度string-
padding分段器内边距string-

Events

事件名说明回调参数
change分段切换时触发index: 当前选中分段的索引
click点击分段时触发index: 点击分段的索引

主题定制

组件提供了以下CSS变量,可用于自定义样式:

变量名默认值说明
--l-segmented-primary-color$primary-color主色(默认激活项边框/文字色)
--l-segmented-bg-color$fill-3背景色
--l-segmented-padding-x$spacer-tn水平内边距
--l-segmented-padding-y$spacer-tn垂直内边距
--l-segmented-radius$border-radius-sm整体圆角大小
--l-segmented-text-color$text-color-1文字颜色
--l-segmented-active-bg-colorwhite激活项背景色
--l-segmented-active-text-color$primary-color激活项文字颜色
--l-segmented-small-height32px小尺寸高度
--l-segmented-small-font-size$font-size-sm小尺寸文字大小
--l-segmented-medium-height36px中尺寸高度
--l-segmented-medium-font-size$font-size中尺寸文字大小
--l-segmented-large-height40px大尺寸高度
--l-segmented-large-font-size$font-size-md大尺寸文字大小

支持与赞赏

如果你觉得本插件解决了你的问题,可以考虑支持作者:

支付宝赞助微信赞助
支付宝微信支付

源代码

组件源码