以下是一个基于uni-app框架,使用顶部选项卡的代码示例。
uni-tab-bar组件,并在组件内部添加多个uni-tab-bar-item组件,来实现顶部选项卡的布局。- <template>
- <view>
- <uni-tab-bar :current="current" @click="onClickTabBar">
- <uni-tab-bar-item icon="home" text="首页">uni-tab-bar-item>
- <uni-tab-bar-item icon="search" text="搜索">uni-tab-bar-item>
- <uni-tab-bar-item icon="user" text="个人">uni-tab-bar-item>
- uni-tab-bar>
- view>
- template>
- export default {
- data() {
- return {
- current: 0, // 当前选中的选项卡
- };
- },
- methods: {
- onClickTabBar(event) {
- this.current = event.detail.index; // 更新当前选中的选项卡
- },
- },
- };
v-if或v-show指令,根据当前选中的选项卡展示相关内容。- <template>
- <view>
- <uni-tab-bar :current="current" @click="onClickTabBar">
- <uni-tab-bar-item icon="home" text="首页">uni-tab-bar-item>
- <uni-tab-bar-item icon="search" text="搜索">uni-tab-bar-item>
- <uni-tab-bar-item icon="user" text="个人">uni-tab-bar-item>
- uni-tab-bar>
- <view v-if="current === 0">首页内容view>
- <view v-if="current === 1">搜索内容view>
- <view v-if="current === 2">个人内容view>
- view>
- template>
通过以上代码,就可以实现一个简单的顶部选项卡,在不同的选项卡中展示不同的内容。需要注意的是,在实际使用中,还需要根据具体的需求对选项卡和内容进行样式和功能的定制。