页面里地方一多,每个 Tab 都手写一遍 wx:if 切换,标题样式和选中态很快就会各不相同。
这里把它抽成一个组件:标题栏由 tabsList 渲染,内容区由 slot 注入,父页面只维护 active。
交互上顺带解决两件事——标题多了可以横向滚动,选中态用半段 / 整段下划线区分。
下面按「背景 → 实现 → 踩坑」展开。
背景
最原始的做法是在页面里写一排 view,点击改 active,再配一段 wx:if 控制内容显隐。页面只有两三个 Tab 时没问题,一旦 Tab 数量增长、又要支持「图标 + 文字」两种标题形态,重复代码就会失控。
抽组件要划清一条边界:标题与内容解耦。组件只认 tabsList(标题数据)和 active(当前选中),内容完全由调用方通过 slot 塞进来。这样组件可以复用在列表筛选、详情切换、分类导航等任何场景,而不用关心内容长什么样。
功能定位:把「标题栏 + 内容区」拆成两个互不耦合的部分——标题栏由 tabsList 渲染,内容区由 slot 注入,父页面只管 active 与内容 wx:if。
实现
1. 属性设计
四个属性覆盖了主要变体:标题数据、当前选中、下划线形态、是否横向滚动。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| Component({ properties: { tabsList: { type: Array, value: [ { img: '/asset/square.jpg', text: '' }, { img: '', text: 'tab1' } ] }, active: { type: Number, value: 0 }, lineState: { type: Number, value: 1 }, tabStatus: { type: Number, value: 0 } } })
|
tabsList 的元素有两种形态:带 img 的走图片模式,否则走文字模式。父页面通过 active 受控,子组件点击后 triggerEvent 通知父级更新。
2. 标题栏渲染
不需要为「滚动 / 静态」写两套结构,一个 scroll-view 就够:滚动能力由 scroll-x 开关决定,静态时子项用 flex: 1 均分,滚动时改成 flex: none 并给固定内边距。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <scroll-view class="tab-bar" scroll-x="{{tabStatus === 1}}" enable-flex="{{tabStatus === 1}}" enhanced show-scrollbar="{{false}}" > <view wx:for="{{tabsList}}" wx:key="index" class="tab-item {{tabStatus === 1 ? 'nojudge' : 'judge'}}" mark:index="{{index}}" bind:tap="onClickTab" > <image wx:if="{{item.img}}" class="tab-img {{active === index ? (lineState ? 'line-full' : 'line') : ''}}" src="{{item.img}}" mode="heightFix" /> <view wx:else class="tab-text {{active === index ? (lineState ? 'line-full' : 'line') : ''}}"> {{item.text}} </view> </view> </scroll-view>
<view class="tab-content"> <slot /> </view>
|
3. 点击切换:用 mark 代替 dataset
mark:index 会把值挂到 e.mark 上,比 data-index + e.currentTarget.dataset 少一层取值,也不会和业务里的 data-* 撞名。
1 2 3 4 5 6 7 8
| methods: { onClickTab(e: WechatMiniprogram.TouchEvent) { const index = Number(e.mark?.index ?? 0) if (index === this.data.active) return this.setData({ active: index }) this.triggerEvent('change', { index }) } }
|
4. 下划线:伪元素做两态
半段、整段两种样式共用同一个伪元素,只改 width 与 left,切换时靠 transition 过渡出滑动感。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| .tab-bar { display: flex; width: 100%; white-space: nowrap; }
.tab-item { display: flex; align-items: center; justify-content: center; min-width: 120rpx; padding: 0 24rpx; box-sizing: border-box; }
.judge { flex: 1; } .nojudge { flex: none; }
.tab-text, .tab-img { position: relative; font-size: 28rpx; color: #666; }
.tab-text::before, .tab-img::before { content: ''; position: absolute; bottom: -8rpx; height: 4rpx; background: #ff8022; transition: left 0.3s, width 0.3s; }
.line::before { left: 50%; width: 20rpx; transform: translateX(-50%); }
.line-full::before { left: 0; width: 100%; }
.tab-content { width: 100%; }
|
5. 父页面怎么用
1 2 3 4
| <custom-tab tabsList="{{tabsList}}" active="{{active}}" tabStatus="{{1}}" lineState="{{1}}" bind:change="onTabChange"> <view wx:if="{{active === 0}}">第一个 Tab 的内容</view> <view wx:elif="{{active === 1}}">第二个 Tab 的内容</view> </custom-tab>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| Page({ data: { active: 0, tabsList: [ { img: '', text: '推荐' }, { img: '', text: '关注' }, { img: '', text: '附近' }, { img: '', text: '直播' } ] }, onTabChange(e: WechatMiniprogram.CustomEvent<{ index: number }>) { this.setData({ active: e.detail.index }) } })
|
踩坑
scroll-view 里的 flex: 1。滚动容器的内容宽度由子项撑开,此时给子项 flex: 1 会退化成「按可用空间均分」,一旦标题总宽超过屏幕就被压缩而不是滚动。所以要按状态切换成 nojudge(flex: none + min-width)。
- 是内容区。组件本身不切换内容,
active 只负责标题高亮;slot 里的内容需要父页面自己用 wx:if / wx:elif 渲染。这一点最容易误解成「传了 tabsList 就自动换内容」。
踩坑清单
scroll-view 缺 scroll-x / enable-flex,横向滑动直接失效。
- 滚动模式下子项仍用
flex: 1,标题被压缩而不是滚动。
- 组件不会自动切换内容,
slot 内容需父页面按 active 自行渲染。
e.mark.index 未做 Number() 校验,非数字会让 active === index 永远不成立。
- 伪元素要显式写
content: '',否则下划线不显示。