首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

uniapp动态设置Tabbar

  • 24-03-18 05:49
  • 2615
  • 12988
blog.csdn.net

一套小程序及app可能会有多个用户角色,多者能看到的内容应该是不一样的。

实现原理
舍弃uniapp原生的tabbar,使用uView插件下的u-tabbar导航插件来实现。
介绍 | uView 2.0 - 全面兼容 nvue 的 uni-app 生态框架 - uni-app UI 框架uView UI,是 uni-app 生态最优秀的 UI 框架,全面的组件和便捷的工具会让您信手拈来,如鱼得水icon-default.png?t=N7T8https://uviewui.com/components/intro.html根据uView 2.0 安装及配置在uniappApp中配置完善,配置完善后进行动态Tabbar设置

效果图

一、设置pages.json

配置需要跳转的路径,不进行图标及名称配置

  1. "tabBar": {
  2. "list": [{
  3. "pagePath": "pages/home/home"
  4. },
  5. {
  6. "pagePath": "pages/admin/admin"
  7. },
  8. {
  9. "pagePath": "pages/me/me"
  10. }
  11. ]
  12. }
二、新建一个common文件夹,并创建文件tabbar.vue

tabbar.vue

  1. <script>
  2. import global from "@/utils/global.js"
  3. export default {
  4. props: ['current', 'tabBarList'],
  5. data() {
  6. return {
  7. }
  8. },
  9. methods: {
  10. getCurrent(){
  11. return global.tabBarCurrent ? global.tabBarCurrent : this.current;
  12. },
  13. tabbatChange(index) {
  14. global.tabBarCurrent = index
  15. uni.switchTab({
  16. url: this.tabBarList[index].pagePath,
  17. })
  18. }
  19. }
  20. }
  21. script>
  22. <style lang="scss" scoped>
  23. image {
  24. width: 20px;
  25. height: 20px;
  26. }
  27. style>

引入的global.js文件中的内容

  1. export default {
  2. timer : null,
  3. tabBarCurrent: 0
  4. }

三、在utils下创建tabbar.js文件并输入配置信息

  1. // 个人用户
  2. const ordinary = [{
  3. icon: '/static/home.png',
  4. inactive: '/static/homeTwo.png',
  5. badge: 0,
  6. text: '首页',
  7. pagePath: "/pages/home/home",
  8. index: '0'
  9. },
  10. {
  11. icon: '/static/me.png',
  12. inactive: '/static/meTwo.png',
  13. badge: 0,
  14. text: '我的',
  15. pagePath: "/pages/me/me",
  16. index: '2'
  17. }
  18. ]
  19. // 企业用户
  20. const member = [{
  21. icon: '/static/home.png',
  22. inactive: '/static/homeTwo.png',
  23. badge: 0,
  24. text: '首页',
  25. pagePath: "/pages/home/home",
  26. index: '0'
  27. },
  28. {
  29. icon: '/static/admin.png',
  30. inactive: '/static/adminTwo.png',
  31. badge: 0,
  32. text: 'admin',
  33. pagePath: "/pages/admin/admin",
  34. index: '1'
  35. },
  36. {
  37. icon: '/static/me.png',
  38. inactive: '/static/meTwo.png',
  39. badge: 0,
  40. text: '我的',
  41. pagePath: "/pages/me/me",
  42. index: '2'
  43. }
  44. ]
  45. export default {
  46. ordinary,
  47. member
  48. }
四、在需要展示tabbar的文件中使用

首页

  1. <script>
  2. export default {
  3. data() {
  4. return {
  5. tabBerLists: []
  6. }
  7. },
  8. onLoad() {
  9. // 影藏原生的tabbar,有自定义tabbar的页面都要写一遍
  10. uni.hideTabBar()
  11. },
  12. onShow() {
  13. this.tabBerLists = uni.getStorageSync('tabBarList') // 自定义的tabbar赋值
  14. },
  15. methods: {
  16. }
  17. }
  18. script>
五、配置vuex,创建store文件夹及index.js文件夹
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex)
  4. import tabBar from '@/utils/tabBar.js' // 引入刚刚创建的tabBar.js
  5. const store = new Vuex.Store({
  6. state: {
  7. tabBarList: [],
  8. },
  9. mutations:{
  10. // 底部tabbar
  11. setRoleId(state,data){
  12. if(data === 1) {
  13. state.tabBarList = tabBar.member
  14. }else {
  15. state.tabBarList = tabBar.ordinary
  16. }
  17. uni.setStorageSync('tabBarList', state.tabBarList) // 根据登录时传过来的值,存储对应的tabbarlist
  18. },
  19. }
  20. })
  21. export default store
六、在登录页使用vuex
  1. <script>
  2. export default {
  3. data() {
  4. return {
  5. }
  6. },
  7. onLoad() {
  8. },
  9. methods: {
  10. login() {
  11. this.$store.commit('setRoleId', 0)
  12. uni.switchTab({
  13. url: '/pages/home/home'
  14. })
  15. }
  16. }
  17. }
  18. script>
  19. <style>
  20. style>
七、在退出登录时,重置global文件中的数据,设置tabBarCurrent 为 0 
  1. <script>
  2. export default {
  3. data() {
  4. return {
  5. }
  6. }
  7. methods: {
  8. signOut() {
  9. this.$global.tabBarCurrent = 0
  10. uni.reLaunch({
  11. url: '/pages/index/index'
  12. })
  13. }
  14. }
  15. }
  16. script>

注:本文转载自blog.csdn.net的_Legend_King的文章"https://blog.csdn.net/qq_63026743/article/details/134924953"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

117
前沿技术
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top