基于vue的uni-app框架 h5移动端-首页多tab实现思路

15 篇文章 0 订阅
13 篇文章 0 订阅

应用首页存在多个顶部导航tab,且每个tab下都有不一样的内容和样式,怎么在不影响数据的情况下,优化用户体验。

头部导航栏+显示区域--

<view class="navbar">
         <view v-for="(item, index) in navList" :key="index" class="nav-item" :class="{ current: tabCurrentIndex === index }"
                 @click="tabClick(index)">
                    {{ item.text }}
           </view>

</view>

<!-- 显示区域 -->
            <view class="list" v-for="(item, index) in navList" :key="index" v-if="tabCurrentIndex === index">

                <!-- 暂未开通 -->
                <view class="lj-errorimg" v-if="index > 0">
                    <image class="did-not" src="../../static/xiaowu.png" mode=""></image>
                </view>

                <!-- 首页 -->
                <view class="" v-if="index == 0">
                    <!-- 轮播图 -->
                    <carouselList :carouselList="carouselList"></carouselList>
                    
                    <!-- 商品列表 -->
                    <goodsList :list="item.list" :navlist="navList" :state="0"></goodsList>
                        
                    <lj-load-more :loadingType="navList[index].loadingType" :contentText="navList[index].contentText"></lj-load-more>
                </view>

            </view>

导航栏js--切换

//顶部tab点击
            tabClick(index) {
                this.tabCurrentIndex = index;

                if (this.navList[index].status == false) {

                    //点击切换tab并调用下拉刷新,请求tab项的数据--当status状态为false时
                    this.getnewsList();
                }
            },

data里的数据--

data() {
            return {
                tabCurrentIndex: 0,
                carouselList: [], //轮播图
                navList: [{
                        text: '首页',
                        loadingText: '加载中...',
                        loadingType: 0,
                        contentText: {
                            contentdown: '',
                            contentrefresh: '正在加载...',
                            contentnomore: '没有更多数据了'
                        },
                        list: [],  //tab第一页的数据
                        page: 1,
                        url: this.GLOBAL + "/api/store/store_domain_list", //第一页请求的接口
                        status: false  //默认状态                   

                    },
                    {
                        text: '商城',
                        // loadingText: '加载中...',
                        // loadingType: 0,
                        // contentText: {
                        //     contentdown:'',
                        //     contentrefresh: '正在加载...',
                        //     contentnomore: '没有更多数据了'
                        // },
                        // list: [],
                        // page: 1,
                        // url: "http://shop_platform.test/api/store/store_domain_list00000",
                        // status: false
                    },
                    {
                        text: '自贸区',
                    },
                    {
                        text: '智造',
                    },
                    {
                        text: '商圈',
                    },
                ],
            };
        },

这里用的上拉加载和下拉刷新为 uni-app插件市场里的,点个赞!

//下拉刷新的时候请求一次数据
        onPullDownRefresh: function() {
            this.getnewsList();
        },
        //触底的时候请求数据,即为上拉加载更多
        onReachBottom: function() {
            this.getmorenews();
        },

下拉刷新--

// 下拉刷新
            getnewsList() {

                //tab的下标
                let index = this.tabCurrentIndex;

                当点击其他几个tab时  停止下拉刷新
                if (index > 0) {
                    setTimeout(function() {
                        uni.stopPullDownRefresh();
                    }, 1000);
                    return;
                }
                this.navList[index].page = 1;
                this.navList[index].loadingType = 0;
                uni.showNavigationBarLoading();
                uni.request({
                    header: {
                        'content-type': 'application/x-www-form-urlencoded' //自定义请求头信息
                    },
                    url: this.navList[index].url,
                    method: 'GET',
                    data: {
                        page: this.navList[index].page,
                        per_page: per_page
                    },
                    success: res => {
                        if (res.data.code == 200000) {
                            // 当请求过一次之后--status变为true,再次点击tab按钮不再请求数据
                            this.navList[index].status = true;

                            if (index == 0) {
                                this.navList[index].page++;
                                this.navList[index].list = res.data.data;
                            }
                        } else {
                            uni.stopPullDownRefresh();
                        }
                    },
                    fail: () => {
                        uni.stopPullDownRefresh();
                    },
                    complete: () => {}
                });
            },

上拉加载--

getmorenews: function() {
                let index = this.tabCurrentIndex;
                if (this.navList[index].loadingType !== 0) { //loadingType!=0;直接返回
                    return false;
                }
                this.navList[index].loadingType = 1;
                uni.showNavigationBarLoading(); //显示加载动画
                uni.request({
                    header: {
                        'content-type': 'application/x-www-form-urlencoded' //自定义请求头信息
                    },
                    url: this.navList[index].url,
                    method: 'GET',
                    data: {
                        page: this.navList[index].page,
                        per_page: per_page
                    },
                    success: res => {
                        if (res.data.code == 200000) {
                            if (res.data.data.data == '') { //没有数据
                                this.navList[index].loadingType = 2;
                                uni.hideNavigationBarLoading(); //关闭加载动画
                                return;
                            }
                            this.navList[index].page++; //每触底一次 page +1

                            if (index == 0) {
                               this.navList[index].list = this.navList[index].list.concat(res.data.data);
                            }
                            this.navList[index].loadingType = 0; //将loadingType归0重置
                            uni.hideNavigationBarLoading(); //关闭加载动画
                        } else {
                            uni.stopPullDownRefresh();
                        }
                    },
                    fail: () => {
                        uni.stopPullDownRefresh();
                    },
                    complete: () => {}
                });
            },

到这里就结束了,接口是本地测试的,这里只展示了第一页的内容,其他页,需要在标签里自己加内容。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您好!如果我理解正确的话,您的问题是如何通过uniApp实现在点击tab选项卡时显示相关数据的功能,对吗? 在uniApp中,您可以使用组件来实现这个功能。具体而言,您可以使用 `uni-TabBar` 组件和 `uni-TabBarItem` 组件来创建一个选项卡布局。然后,您可以在每个选项卡上放置一个页面,用于显示相关数据。 以下是一些示例代码,帮助您实现这个功能: ``` <!-- 在 pages/index/index.vue--> <template> <view class="container"> <uni-tab-bar :active="active" @click="onTabClick"> <uni-tab-bar-item title="选项卡一" icon="home"> <view> <text>这是选项卡一的内容</text> </view> </uni-tab-bar-item> <uni-tab-bar-item title="选项卡二" icon="search"> <view> <text>这是选项卡二的内容</text> </view> </uni-tab-bar-item> <uni-tab-bar-item title="选项卡三" icon="setting"> <view> <text>这是选项卡三的内容</text> </view> </uni-tab-bar-item> </uni-tab-bar> </view> </template> <script> export default { data() { return { active: 0 } }, methods: { onTabClick(index) { this.active = index; } } } </script> ``` 在上面的示例代码中,我们使用了 `uni-tab-bar` 组件和 `uni-tab-bar-item` 组件来创建了一个带有三个选项卡的布局。在每个选项卡中,我们放置了一个 `view` 组件,并在其中显示了相应的文本。 此外,我们还通过 `@click` 事件监听器来实现了点击选项卡时更新 `active` 数据的功能。通过更新 `active` 数据,我们可以让页面显示相应选项卡中的内容。 希望这个回答能够帮助到您!如果您还有其他问题,请随时提问。 ### 回答2: uni-app是一个跨平台的开发框架,可以同时构建iOS和安卓应用,并且可以在H5和小程序平台上运行。实现点击tab选项卡展现该状态的数据,可以通过以下步骤来实现: 1. 在uni-app的页面中,使用tab组件来创建选项卡。可以根据自己的需求来设置选项卡的数量和文本内容,同时可以绑定一个变量来标识当前选中的选项卡。 2. 在每个选项卡对应的页面中,可以绑定不同的数据状态。可以通过在data中定义不同的变量来存储不同状态下的数据。例如可以定义一个变量data1来存储选项卡1的数据,data2来存储选项卡2的数据,以此类推。 3. 在点击选项卡时,可以通过事件监听来切换选项卡的状态。可以在每个选项卡对应的页面中定义一个方法,当选项卡被点击时,调用这个方法来改变变量的值,从而切换选项卡的状态。 4. 根据选项卡的状态,可以在页面中显示对应状态下的数据。可以通过条件判断来展示不同状态下的数据。例如可以使用v-if指令来判断当前选项卡的状态,然后根据状态来展示不同的数据。 通过以上步骤,我们可以实现点击tab选项卡展示该状态的数据。根据不同选项卡的状态,可以在页面中展示对应状态下的数据,从而满足不同业务需求。uni-app的跨平台特性使得我们可以快速开发并在多个平台上运行应用,方便开发者进行应用的功能实现和优化。 ### 回答3: uniApp是一种基于Vue.js的跨平台开发框架,可以用于开发多个平台的应用程序。在uniApp中,可以通过使用tab选项卡组件来展现不同状态的数据。 首先,我们需要在uniApp的页面中引入tab选项卡组件,并设置相关的属性和事件。在引入组件之后,我们可以使用v-for指令来遍历数据列表,并将每个选项卡的标题和内容与相应的数据绑定。例如: <template> <view> <uni-tabs v-model="activeTab" :tabs="tabs" @change="handleChange"> <uni-tab-pain v-for="(item, index) in tabs" :key="index" :label="item.label"> <view>{{ item.content }}</view> </uni-tab-pain> </uni-tabs> </view> </template> <script> export default { data() { return { activeTab: 0, // 默认选中的选项卡索引 tabs: [ // 选项卡列表数据 { label: '状态1', content: '状态1的数据内容' }, { label: '状态2', content: '状态2的数据内容' }, { label: '状态3', content: '状态3的数据内容' } ] } }, methods: { handleChange(index) { console.log('当前选项卡索引:', index) // 根据index获取对应的状态数据 // 更新页面中展示的数据 } } } </script> 在页面中,我们使用了uni-tabs和uni-tab-pain两个组件来实现选项卡的效果。uni-tabs是整个选项卡组的容器,而uni-tab-pain是每个选项卡的内容区域。 在data中,我们定义了activeTab来标识当前选中的选项卡索引,同时定义了tabs数组来存储所有选项卡的数据。 在methods中,我们定义了handleChange方法来处理选项卡切换事件。在切换选项卡时,handleChange方法会被触发,并接收当前选项卡的索引作为参数。我们可以根据这个索引获取对应的状态数据,并更新页面中展示的数据内容。 总结一下,通过使用uniApp的tab选项卡组件,我们可以实现点击选项卡展现不同状态的数据。在切换选项卡时,可以通过事件处理方法来更新页面中展示的数据内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值