介绍:html
如今咱们要知足的项目需求是根据登陆用户角色的不一样,tabBar的数量和内容显示不一样,以下图vue
用户角色为管理员时:
用户为普通用户时:vuex
注意:如下全部的设置在uView框架下的微信小程序中使用没有问题,其余的小程序客户端或者框架何尝试
"tabBar": { "custom": true, "color": "#909399", "selectedColor": "#303133", "backgroundColor": "#FFFFFF", "borderStyle": "black", "list":[ { "pagePath": "pages/index/index", "iconPath": "static/uview/tabs/discount.png", "selectedIconPath":"static/uview/tabs/discount_selected.png", "text": "优惠" }, { "pagePath": "pages/category/category", "iconPath": "static/uview/tabs/products.png", "selectedIconPath": "static/uview/tabs/products_selected.png", "text": "商品" }, { "pagePath": "pages/user/user", "iconPath": "static/uview/tabs/user.png", "selectedIconPath": "static/uview/tabs/user_selected.png", "text": "用户中心" } ] }, "usingComponents": {}
// 普通的用户 const generalUser = [ { text: "优惠", pagePath: "/pages/index/index", iconPath: "/static/uview/tabs/discount.png", selectedIconPath: "/static/uview/tabs/discount_selected.png" }, { text: "商品", pagePath: "/pages/category/category", iconPath: "/static/uview/tabs/products.png", selectedIconPath: "/static/uview/tabs/products_selected.png" }, { text: "个人", pagePath: "/pages/user/user", iconPath: "/static/uview/tabs/user.png", selectedIconPath: "/static/uview/tabs/user_selected.png" } ] // 小程序管理者 const admin = [ { text: "优惠", pagePath: "/pages/index/index", iconPath: "/static/uview/tabs/discount.png", selectedIconPath: "/static/uview/tabs/discount_selected.png" } ] // 配送员 const courier = [ { text: "首页", pagePath: "/pages/index/index", iconPath: "/static/uview/tabs/discount.png", selectedIconPath: "/static/uview/tabs/discount_selected.png" }, { text: "个人", pagePath: "/pages/user/user", iconPath: "/static/uview/tabs/user.png", selectedIconPath: "/static/uview/tabs/user_selected.png" } ] export default { generalUser, admin, courier }
这里须要注意配置页面的顺序和图片资源的路径,这里的顺序是展现时的顺序,图片路径static前必须加上/,不然会找不到
uniapp框架集成了vuex,能够直接使用json
import tabBar from "../../utils/tabBar.js" // 判断当前登录用户角色 // 0 为管理员 // 1 为普通用户 // 2 为快递员 // 三元表达式判断当前登录的用户角色 var user_type = uni.getStorageInfoSync("userType") let type = user_type === 0 ? 'admin': user_type === 1 ? "generalUser" : "courier" const state = { list: tabBar[type] } export default { namespaced: true, state }
const getters = { tabBarList: state => state.tabBar.list, } export default getters
import getters from './getters.js' import tabBar from './modules/tabBar.js' const store = new Vuex.Store({ modules: { tabBar }, getters }
import store from '@/store'; const app = new Vue({ store });
在每一个须要展现tabBar的页面引入uView中的tabBar组件,父传子传值,一些固定的值能够不传值直接在组件中修改小程序
// template部分,u-tabBar放在页面展现最后 <u-tabbar :list="tabBarList" :active-color="activeColor" :inactive-colot="inactiveColor" ></u-tabbar> // js部分 <script> import { mapGetters } from 'vuex' export default { data() { return { activeColor: "#fa3534", // 选中时的颜色 inactiveColor: "#000" // 未选中时的颜色 } }, computed: { ...mapGetters([ 'tabBarList', // 获取展现的tabBar列表 ]) } } </script>
完结!微信小程序
文章内容主要引自 https://chowdera.com/2021/01/...,只是在文章中修改了代码、图片以及少部分注意事项。