uniapp自定义底部导航tabBar,实现不同权限不同tabBar

文章介绍了如何在uni-app中根据用户角色展示不同tabbar的实现步骤,包括在page.json中配置自定义tabbar,创建并填充tabbarData.js,配置store进行角色判断,以及在tabbar.vue组件中使用数据并监听变化。同时展示了在页面中引入自定义tabbar组件的方法。
摘要由CSDN通过智能技术生成


问题与需求

需求:有多个角色,需要通过判定角色来展示相对应的不同tabbar。

问题:如果用自带的tabbar只能写一个tabbar(包括五项)

解决:采用自定义tabbar


解决

一、在page.json中配置tabbar

注意1:这里list只能放5个path,所以如果要有不同的tabbar就需要在这个index里面在根据不同的权限调用不同的界面(不同权限对应的界面此时作为子组件出现,而不是跳转)
简单做文件结构示例
在这里插入图片描述
这里是index里面根据角色选择子界面(组件)的示例
在这里插入图片描述

注意2:“custom”: true,说明启用自定义tabbar

下面是配置的具体代码

"tabBar": {
    "custom": true,
    "color": "#666",
    "selectedColor": "#6847f5",
    "borderStyle": "white",
    "list": [
      {
        "pagePath": "pages/fourth/index"
      },
      {
        "pagePath": "pages/home/index"
      },
      {
        "pagePath": "pages/warning/index"
      },
      {
        "pagePath": "pages/data/index"
      },
      {
        "pagePath": "pages/userInfo/index"
      }
    ]
  }

二、创建自定义tabbarData.js文件

创建位置在utils文件夹下面。这里记录了每种tabbar的信息。

**注意1:**这里的页面路径必须是上面配置的路径,因为只有在page.json中配置过的路径才能用uni.switchTab跳转。

**注意2:**顺序就是从左到右显示的顺序。

代码示例如下:(由于源代码太长,这里只放了两个角色的简化版)

const teacherTabList = [
  {
    // 未点击图标
    iconPath: require('@/static/images/icon/icon1.png'),
    // 点击后图标
    selectedIconPath: require('@/static/images/icon/icon2.png'),
    // 显示文字
    text: '健康',
    // 是否使用自定义图标
    customIcon: true,
    // 页面路径
    pagePath: '/pages/warning/index'
  },
  {
    iconPath: require('@/static/images/icon/icon3.png'),
    selectedIconPath: require('@/static/images/icon/icon4.png'),
    text: '家校',
    customIcon: true,
    pagePath: '/pages/home/index'
  }
];
const patriarchTabList = [
  {
    iconPath: require('@/static/images/icon/icon19.png'),
    selectedIconPath: require('@/static/images/icon/icon20.png'),
    text: '警报',
    customIcon: true,
    pagePath: '/pages/warning/index'
  },
  {
    iconPath: require('@/static/images/icon/icon3.png'),
    selectedIconPath: require('@/static/images/icon/icon4.png'),
    text: '家校',
    customIcon: true,
    pagePath: '/pages/home/index'
  }
];
export default {
  teacherTabList,
  patriarchTabList
};

三、配置store

目的:在全局使用(主要是自定义tabbar那个文件)经过这里确定的tabbarlist
以下示例也只保留了关键内容

import tabBer from "@/utils/tabbarData";
let type = uni.getStorageSync("role") === 1 ? "teacherTabList" : "patriarchTabList"
const state = {
  list: tabBer[type],
};
const mutations = {
  putTabBerList(state, value) {
    state.list = value;
  },
  putUserMsg(state, value) {
    state.userMsg = value;
  },
};
const actions = {
  async putUserMsg({ commit }, value) {
    const data = await userDetail();
    if (!data || (!data.name && !data.phone)) {
      if (!uni.getStorageSync("userFlag")) {
        uni.redirectTo({ url: "/subPages/bindInfo/index" });
      }
      return false;
    } else {
      commit("putUserMsg", value);
      if (!uni.getStorageSync("userFlag")) {
        switch (uni.getStorageSync("role")) {
          case 1:
            commit("putTabBerList", tabBer["teacherTabList"]);
            uni.switchTab({ url: "/pages/warning/index" });
            break;
          case 2:
            commit("putTabBerList", tabBer["patriarchTabList"]);
            uni.switchTab({ url: "/pages/data/index" });
            break;
        }
      }
    }
  },
};
const getters = {
  tabBerList: (state) => state.list
};

export default {
  state,
  getters,
  actions,
  mutations,
};

四、在自定义tabbar(tabbar.vue)中使用

注意: 使用…mapGetters([‘tabBerList’]) 使得在自定义tabbar的文件中能检测到实时的tabbarlist是哪一种并使用。

顺便附上自定义tabbar的代码。实现的效果:有选中和非选中图标的区别

<template>
  <view class="tabbar-style">
    <u-tabbar
      :value="value"
      :fixed="true"
      :placeholder="true"
      :border="false"
      activeColor="#6847F5"
      inactiveColor="#ADA9C1"
      :safeAreaInsetBottom="true"
    >
      <u-tabbar-item
        v-for="(item, index) of tabBerList"
        :key="index"
        :text="item.text"
        @click="clickTabChange(item, index)"
      >
        <image class="tab-icon" slot="active-icon" :src="item.iconPath"></image>
        <image
          class="tab-icon"
          slot="inactive-icon"
          :src="item.selectedIconPath"
        ></image>
      </u-tabbar-item>
    </u-tabbar>
  </view>
</template>

<script>
import { mapGetters } from 'vuex';
export default {
  props: {//使用tabbar的界面的传参,value决定了是tabbar的第几个正在被选中。
    value: Number
  },
  computed: {
    ...mapGetters(['tabBerList'])
  },
  methods: {
    clickTabChange(e, i) {
      uni.switchTab({ url: e.pagePath });
    }
  }
};
</script>
<style lang="less" scoped>
.tabbar-style {
  position: fixed;
  bottom: 0;
  width: 100%;
  z-index: 9;
}
.tab-icon {
  .square(38rpx; 38rpx);
}
</style>

五、在界面中使用自定义tabbar作为组件

注意: 其中传入的value是从0开始计的,代表了选中了当前tabbar的第几个

<template>
  <view>
	...
    <tab-bar :value="2" />
  </view>
</template>
<script>
import TabBar from '@/components/TabBar';
components: { TabBar },
</script>
  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你可以使用uni-app提供的自定义组件和样式来实现悬浮底部tabbar。以下是一个简单的示例: 1. 创建一个自定义组件,例如命名为`custom-tabbar`。 2. 在`custom-tabbar`组件中,使用`fixed`定位和负的`bottom`值来实现悬浮在底部。 3. 在`custom-tabbar`组件中,添加tabbar的选项卡,并处理点击事件。 4. 在页面中引入`custom-tabbar`组件,并将选项卡的数据传递给组件。 下面是示例代码: ```vue <!-- custom-tabbar.vue --> <template> <view class="custom-tabbar"> <view class="tabbar-item" v-for="(item, index) in tabbarList" :key="index" @click="handleClick(index)"> <image :src="item.icon"></image> <text>{{ item.title }}</text> </view> </view> </template> <script> export default { data() { return { tabbarList: [ { title: '首页', icon: 'home' }, { title: '消息', icon: 'message' }, { title: '我的', icon: 'user' } ] }; }, methods: { handleClick(index) { // 处理点击事件 console.log('点击了第', index + 1, '个选项卡'); } } }; </script> <style> .custom-tabbar { position: fixed; bottom: -10px; /* 负的bottom值,使其悬浮在底部 */ width: 100%; height: 50px; background-color: #fff; box-shadow: 0px -2px 4px rgba(0, 0, 0, 0.1); display: flex; justify-content: space-around; align-items: center; } .tabbar-item { display: flex; flex-direction: column; align-items: center; } .tabbar-item image { width: 24px; height: 24px; } .tabbar-item text { font-size: 12px; margin-top: 6px; } </style> ``` 在页面中使用`custom-tabbar`组件: ```vue <!-- index.vue --> <template> <view> <!-- 页面内容 --> <view>内容</view> <!-- 引入自定义tabbar组件 --> <custom-tabbar></custom-tabbar> </view> </template> <script> import CustomTabbar from '@/components/custom-tabbar.vue'; export default { components: { CustomTabbar } }; </script> <style> /* 页面样式 */ </style> ``` 这样,你就可以实现一个自定义悬浮底部tabbar了。你可以根据自己的需求来修改`custom-tabbar`组件的样式和功能。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值