当前位置: 首页 > Web前端 > vue.js

微信小程序下根据用户不同身份显示不同标签栏和页面(uniapp)

时间:2023-03-31 16:44:25 vue.js

当您是管理员时:当用户是普通用户时:注意:以下所有设置都可以在微信小程序下使用uView框架。其他小程序客户端或框架没试过。1、配置tabBar,创建tabBar中需要展示的所有页面,包括不同角色展示的不同页面,打开pages.json页面配置tabBar项。所有需要在tabBar出现的页面都需要在这里配置。需要特别注意的是:custom必须配置为true,如果不配置,在打开小程序的时候,会先加载内置的tabBar,然后再加载自定义的tabBar,会影响效果使用"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":"Offer"},{"pagePath":"pages/category/category","iconPath":"static/uview/tabs/products.png","selectedIconPath":"static/uview/tabs/products_selected.png","文本":"产品"},{"pagePath":"pages/user/user","iconPath":"static/uview/tabs/user.png","selectedIconPath":"static/uview/tabs/user_selected.png","text":"UserCenter"}]},"usingComponents":{}创建一个utils目录,并在该目录下新建一个tabBar.js文件。该文件用于自定义tabBar属性文件覆盖内置配置//普通用户constgeneralUser=[{text:"Discount",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:"My",pagePath:"/pages/user/user",iconPath:"/static/uview/tabs/user.png",selectedIconPath:"/static/uview/tabs/user_selected.png"}]//小程序管理器constadmin=[{text:"Offer",pagePath:"/pages/index/index",iconPath:"/static/uview/tabs/discount.png",selectedIconPath:"/static/uview/tabs/discount_selected.png"}]//送货员constcourier=[{text:"Home",pagePath:"/pages/index/index",iconPath:"/static/uview/tabs/discount.png",selectedIconPath:"/static/uview/tabs/discount_selected.png"},{text:"My",pagePath:"/pages/user/user",iconPath:"/static/uview/tabs/user.png",selectedIconPath:"/static/uview/tabs/user_selected.png"}]导出默认值{generalUser,admin,courier}这里需要注意配置页面的顺序和图片资源的路径。这里的顺序是显示的顺序。图片路径static前一定要加/,否则找不到。2.配置vuexuniapp框架集成安装vuex后,可以直接在store目录下新建一个modules文件夹,在modules文件夹下新建tabBar.js文件。该文件用于判断不同的登录角色,根据结果显示不同的tabBar和页面。importtabBarfrom"../../utils/tabBar.js"//判断当前登录用户角色//0为管理员//1为普通用户//2为快递员//三元表达式确定当前登录varuser_type=uni.getStorageInfoSync("userType")lettype=user_type===0?'管理员':user_type===1?"generalUser":"courier"conststate={list:tabBar[type]}exportdefault{namespaced:true,state}在store目录新建getters.js文件,配置constgetters={tabBarList:state=>state.tabBar.list,}exportdefaultgetters配置index.js文件importinthestore目录gettersfrom'./getters.js'importtabBarfrom'./modules/tabBar.js'conststore=newVuex.Store({modules:{tabBar},getters}配置main.js文件,引入stroeimportstorefrom'@/store';constapp=newVue({store});3.导入组件在每个页面导入uView中的tabBar组件需要显示tabBar,parent给child传值,有些固定值可以不传值直接导入修改组件中的//template部分,将u-tabBar放在页面末尾显示//js部分