Uniapp小程序开发-底部tabbar的开发思路

文章目录


前言

记录uniapp 开发小程序的底部tabbar ,这里讨论的不是自定义tabbar的情况。而是使用wx.setTabBarItem(Object object) 这个api的情况。关于custom 小程序底部tabbar的可以跳过。


一、uniapp 实现 tabbar

实现 tabbar 的简单步骤如下:

  1. 在 uniapp 项目的 pages.json 文件中,配置 tabBar 字段,来定义 tabbar 的样式和内容,例如:
json 复制代码
{
  "tabBar": {
    "color": "#666",
    "selectedColor": "#f00",
    "backgroundColor": "#fff",
    "borderStyle": "black",
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页",
        "iconPath": "static/tabbar/home.png",
        "selectedIconPath": "static/tabbar/home_active.png"
      },
      {
        "pagePath": "pages/cart/cart",
        "text": "购物车",
        "iconPath": "static/tabbar/cart.png",
        "selectedIconPath": "static/tabbar/cart_active.png"
      },
      {
        "pagePath": "pages/user/user",
        "text": "个人中心",
        "iconPath": "static/tabbar/user.png",
        "selectedIconPath": "static/tabbar/user_active.png"
      }
    ]
  }
}
  1. pages 目录下创建对应的三个页面:indexcartuser

  2. pages/index 目录下创建 index.vue 文件,编写首页的内容。

  3. pages/cart 目录下创建 cart.vue 文件,编写购物车页面的内容。

  4. pages/user 目录下创建 user.vue 文件,编写个人中心页面的内容。

  5. App.vue 文件中,将 tabbar 的内容放在 <tabbar> 标签内,例如:

html 复制代码
<template>
  <view>
    <router-view></router-view>
    <tabbar></tabbar>
  </view>
</template>
  1. 在项目的根目录下,创建 components 文件夹,再在该文件夹下创建 tabbar.vue 文件,编写 tabbar 的样式和逻辑,例如:
html 复制代码
<template>
  <view class="tabbar">
    <view v-for="(item, index) in tabBar.list" :key="index" class="tabbar-item" @click="switchTab(index)">
      <image :src="item.selected ? item.selectedIconPath : item.iconPath" class="tabbar-icon"></image>
      <view class="tabbar-text">{{ item.text }}</view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      tabBar: getApp().globalData.tabBar  // 从全局数据中获取 tabBar 的配置
    }
  },
  methods: {
    switchTab(index) {
      uni.switchTab({
        url: '/' + this.tabBar.list[index].pagePath
      })
    }
  }
}
</script>

<style scoped>
.tabbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 50px;
  background-color: #fff;
  border-top: 1px solid #000;
}

.tabbar-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: #666;
}

.tabbar-icon {
  width: 24px;
  height: 24px;
}

.tabbar-text {
  margin-top: 2px;
}
</style>
  1. main.js 文件中全局配置 tabBar 的配置,例如:
javascript 复制代码
App({
  globalData: {
    tabBar: require('./pages.json').tabBar
  }
})

这样,就完成了 tabbar 的简单实现过程。tabbar 的样式和逻辑可以根据项目的需求进行自定义修改,tabbar第一个path与pages的path 保持一致。也就是首页路径。

二、图标使用网络图片

那么如果是icon是后端返回网络图片,url的形式。

json 复制代码
{
  "iconPath": "https://files/static/tabbar/home.png",
  "selectedIconPath": "https://files/static/tabbar/home_active.png"
}
     

注意这个功能要小程序版本的基础库版本在 2.7.0以上。

后端返回tabbar信息

后端返回tabbar信息,已达到可以在后端管理tabbar的图标。即是iconPath,selectedIconPath 这些是支持重置。官方提供了这个apiwx.setTabBarItem(Object object)

uniapp方式中的setTabBarItem

如图在onLauch中加入相关逻辑。获取tabbar信息后,uni.setTabBarItem。


总结

今天的内容就在这里了,本文讨论如何使用uniapp实现基本tabbar功能,接着给出动态设置icon的思路。依赖的api是uni.setTabBarItem。

相关推荐
ooolmf几秒前
matlab2024读取温度01
java·前端·javascript
打工人小夏2 分钟前
前端vue3项目使用nprogress动画组件,实现页面加载动画
前端
一颗宁檬不酸4 分钟前
前端农业商城中产品产地溯源功能的实现
前端
李少兄11 分钟前
深入理解前端中的透视(Perspective)
前端·css
席之郎小果冻13 分钟前
【03】【创建型】【聊一聊,单例模式】
开发语言·javascript·单例模式
江公望21 分钟前
HTML5 History 模式 5分钟讲清楚
前端·html·html5
云和数据.ChenGuang27 分钟前
Zabbix Web 界面安装时**无法自动创建配置文件 `zabbix.conf.php`** 的问题
前端·zabbix·运维技术·数据库运维工程师·运维教程
码界奇点30 分钟前
Java Web学习 第15篇jQuery万字长文详解从入门到实战解锁前端交互新境界
java·前端·学习·jquery
前端老曹39 分钟前
vue3 三级路由无法缓存的终极解决方案
前端·javascript·vue.js·vue