当前位置:   article > 正文

uniapp自定义tabBar_uniapp封装自定义tabbar

uniapp封装自定义tabbar

uniapp自定义tabBar方案

 

该方案,可以在切换tabBar的时候,路由也跟着变化,方便平时进行页面测试使用,不过有个缺点,第一次切换的时候会出现闪烁现象。

解决闪烁的问题:

1、可以把tabBar和页面组件都放在单页中用v-if判断,进行切换,单页切换的缺点是不能进行路由跳转(不能路由跳转)

  1. <template>
  2. <view>
  3. <index></index>
  4. <my></my>
  5. <team></team>
  6. <promotion></promotion>
  7. <tab-bar></tab-bar>
  8. </view>
  9. </view>
  10. </template>

2、把组件注入App.vue中,实现全局引用,但是uniapp不建议在App.vue中使用页面组件,也不要使用,不然出现各种问题,uniapp不能像vue那样在App.vue使用router-view实现选项卡功能(不推荐)

3、 所以要同时实现首次不闪烁,而且可以进行路由切换的功能,目前不知道如何处理。(有个取巧的方式就是用uni-transition包裹一下,使得第一次加载有个过渡效果)

首次加载闪烁,严格意义上说,可以被认定为bug。小编在想能不能用vue的路由方式去实现router-view呢?因此尝试的去自定义router-view,却发现uniapp内置了路由,当想用vue插件方式,重写路由时,发现冲突了。

其实关于自定义,在uniapp中,官方目前不推荐大家去自定义的,如果业务场景需要根据不同权限,显示不同tabBar,我们可以去跟需求谈,换另外一方式,也是可以的。例如用下图的方式 

 

(方案还有需要完善的地方,这里仅供大家参考) 

一、pages.json文件中添加tarBar

因为需要用到tabBar跳转,因此可以往原来的tabBar.list中多添加几个,用于做判断是否tabBar切换(这里可以只添加路径,icon和text可以不需要)

 

二、把原生的tabBar隐藏起来

在App.vue中把原生的tabBar先隐藏起来,同时添加一个removeStorageSync,用于移除使用的tabBar的下标。

  1. onLaunch: function() {
  2. console.log('App Launch')
  3. uni.hideTabBar()
  4. uni.removeStorageSync('selectedIndex');
  5. },

三、自定义一个tabBar组件

在components中新建一个tabBar.vue的页面,用来封装组件

HTML代码,这里循环遍历自己定义的tabBar.list(可根据需求来定义)

这里用了uni-transition组件包裹,这个组件需要到插件市场自行安装,这里用uni-transition是为了让加载的时候闪烁不那么明显。

  1. <template>
  2. <uni-transition mode-class="fade" :duration="200" :show="true">
  3. <view>
  4. <view class="tab-content">
  5. <slot />
  6. </view>
  7. <view class="tabbar">
  8. <view class="navigator">
  9. <view ref='warpper' class="warpper">
  10. <view ref="navItem" class="navigator-item" v-for="(item,index) in tabBar.list"
  11. :key="item.pagePath" @click="switchTab(item,index)" :data-index='index'>
  12. <img :src="item.iconPath" class="icon" v-if="selectedIndex !== index">
  13. <img :src="item.selectedIconPath" class="icon" v-else>
  14. <text :class="['item-text',{'text-active':selectedIndex === index}]">{{item.text}}</text>
  15. </view>
  16. </view>
  17. </view>
  18. </view>
  19. </view>
  20. </uni-transition>
  21. </template>

 data里面定义变量和循环列表,list即是自己定义的tabBar,和pages.json的tabBar写法一样

  1. data() {
  2. return {
  3. selectedIndex: uni.getStorageSync('selectedIndex') || 0, // 标记
  4. tabBar: {
  5. list: [{
  6. "pagePath": "pages/index/index",
  7. "iconPath": "/static/tabIcon/icon1.png",
  8. "selectedIconPath": "/static/tabIcon/icon2.png",
  9. "text": "首页"
  10. }, {
  11. "pagePath": "pages/team/team",
  12. "iconPath": "/static/tabIcon/icon3.png",
  13. "selectedIconPath": "/static/tabIcon/icon4.png",
  14. "text": "团队"
  15. }, {
  16. "pagePath": "pages/promotion/promotion",
  17. "iconPath": "/static/tabIcon/icon5.png",
  18. "selectedIconPath": "/static/tabIcon/icon6.png",
  19. "text": "推广圈"
  20. },
  21. {
  22. "pagePath": "pages/my/my",
  23. "iconPath": "/static/tabIcon/icon7.png",
  24. "selectedIconPath": "/static/tabIcon/icon8.png",
  25. "text": "我的"
  26. }
  27. ]
  28. },
  29. }
  30. },

 如果需要根据不同用户来渲染不同tabBar,可用vuex来保存tabBar的list列表

  1. const user_1 = [{
  2. "pagePath": "../pages/test/me",
  3. "iconPath": "../static/xxx.png",
  4. "selectedIconPath": "../static/xxx.png",
  5. "text": "me"
  6. }, {
  7. "pagePath": "../pages/test/you",
  8. "iconPath": "../static/xxx.png",
  9. "selectedIconPath": "../static/xxx.png",
  10. "text": "you"
  11. }, {
  12. "pagePath": "../pages/test/other",
  13. "iconPath": "../static/xxx.png",
  14. "selectedIconPath": "../static/xxx.png",
  15. "text": "other"
  16. },
  17. ]
  18. const user_2 = [{
  19. "pagePath": "../pages/test/our",
  20. "iconPath": "../static/xxx.png",
  21. "selectedIconPath": "../static/xxx.png",
  22. "text": "our"
  23. }, {
  24. "pagePath": "../pages/test/his",
  25. "iconPath": "../static/xxx.png",
  26. "selectedIconPath": "../static/xxx.png",
  27. "text": "his"
  28. },
  29. ]
  30. export default {
  31. user_1,
  32. user_1
  33. }

四、全局引用组件

往main.js注入组件

  1. // main.js
  2. import tabBar from 'components/tab-bar/tabBar.vue'
  3. Vue.component('tab-bar',tabBar) //挂载

在每一个页面中引入

  1. <template>
  2. <view>
  3. <view class="">我是首页</view>
  4. <tab-bar ref='tabbar'></tab-bar>
  5. </view>
  6. </template>
  1. <template>
  2. <view>
  3. <view class="">我是推广圈页</view>
  4. <tab-bar ref='tabbar'></tab-bar>
  5. </view>
  6. </template>
  1. <template>
  2. <view>
  3. <view class="">我是团队页</view>
  4. <tab-bar ref='tabbar'></tab-bar>
  5. </view>
  6. </template>

五、路由跳转

tabBar定义成了组件,因此需要一个全局变量来判断它的切换和路由的跳转。

首先,进入到uniapp的官方tabBar源码中,可以看到这样一段代码,下面的这段代码,就是路由跳转的依据,该监听方式只能在uniapp中有效,在app中无法监听,因此我们需要自定义跳转方法

  1. watch: {
  2. $route: {
  3. immediate: true,
  4. handler (to) {
  5. if (to.meta.isTabBar) { // 判断是否属于tarBar
  6. // this.__path__ = to.path
  7. // 判断路由的路径和tarBar.list的pagePath是否相同,来确定一个全局的index
  8. const index = this.list.findIndex(item => to.meta.pagePath === item.pagePath)
  9. if (index > -1) {
  10. this.selectedIndex = index // 标记是第几个tarBar
  11. }
  12. }
  13. }
  14. }
  15. },

后续要使用switchTab进行跳转的话,就需要到自定义的方法,毕竟已经自定义了tabBar,跳转方式也需要自定义。

自定义switchTab

  1. switchTab(item, index) {
  2. const navItem = this.$refs.navItem
  3. let url = '/' + item.pagePath
  4. let pagePath = url
  5. uni.switchTab({
  6. url
  7. })
  8. this.tabBar.list.forEach((v, i) => {
  9. if (item.pagePath === v.pagePath) {
  10. uni.setStorageSync('selectedIndex', index); // 注:下标需要异步保存起来
  11. }
  12. })
  13. }

父组件中使用ref="tabBar"来获取子组件 

  1. <template>
  2. <view>
  3. <view class="">我是首页</view>
  4. <button @click="toMY">跳转</button>
  5. <tab-bar ref="tabBar"></tab-bar>
  6. </view>
  7. </template>

通过方法来触发子组件方法即可, 对象中的pagePath是与tabBar.list数组里面的pagePath是对应的。

  1. toMY() {
  2. this.$refs.tabBar.switchTab({
  3. pagePath:'pages/team/team'
  4. })
  5. // 注:这里需要把下标也写上,不然无法命中
  6. uni.setStorageSync('selectedIndex', 5);
  7. },

在app中$route找不到的问题

打包到小程序之后,$route会像下面图片那样显示undefined,即在小程序中,是没有像$route这样的路由方式。那么,就不能用同步的方式,来监听index的变化。

需要用uni.setStorageSync('selectedIndex', index);  

  1. switchTab(item, index) {
  2. // this.selectedIndex = index // 这样写是无效的
  3. this.tabBar.list.forEach((v, i) => {
  4. if (item.pagePath === v.pagePath) {
  5. uni.setStorageSync('selectedIndex', index); // 注:下标需要异步保存起来
  6. }
  7. })
  8. }

最后进入App.vue文件中,清除一下selectedIndex的缓存,不在tabBar.vue的created生命周期清除selectedIndex的原因是:只希望,每次重新进入程序后重置,tabBar的高亮重新回到第一个,而不是每次进入页面的重置,而且每次进入页面重置,是有问题。

  1. <script>
  2. export default {
  3. onLaunch: function() {
  4. uni.removeStorageSync('selectedIndex'); // 清除缓存
  5. uni.hideTabBar()
  6. },
  7. }
  8. </script>

六、相关代码

在App.vue中把原生的tabBar先隐藏起来和清除index

  1. onLaunch: function() {
  2. console.log('App Launch')
  3. uni.removeStorageSync('selectedIndex'); // 清除缓存
  4. uni.hideTabBar()
  5. },

tabBar.vue

  1. <template>
  2. <uni-transition mode-class="fade" :duration="200" :show="true">
  3. <view>
  4. <view class="tab-content">
  5. <slot />
  6. </view>
  7. <view class="tabbar">
  8. <view class="navigator">
  9. <view ref='warpper' class="warpper">
  10. <view ref="navItem" class="navigator-item" v-for="(item,index) in tabBar.list"
  11. :key="item.pagePath" @click="switchTab(item,index)" :data-index='index'>
  12. <img :src="item.iconPath" class="icon" v-if="selectedIndex !== index">
  13. <img :src="item.selectedIconPath" class="icon" v-else>
  14. <text :class="['item-text',{'text-active':selectedIndex === index}]">{{item.text}}</text>
  15. </view>
  16. </view>
  17. </view>
  18. </view>
  19. </view>
  20. </uni-transition>
  21. </template>
  22. <script>
  23. export default {
  24. data() {
  25. return {
  26. selectedIndex: uni.getStorageSync('selectedIndex') || 0,
  27. tabBar: {
  28. list: [{
  29. "pagePath": "pages/index/index",
  30. "iconPath": "/static/tabIcon/icon1.png",
  31. "selectedIconPath": "/static/tabIcon/icon2.png",
  32. "text": "首页"
  33. }, {
  34. "pagePath": "pages/team/team",
  35. "iconPath": "/static/tabIcon/icon3.png",
  36. "selectedIconPath": "/static/tabIcon/icon4.png",
  37. "text": "团队"
  38. }, {
  39. "pagePath": "pages/promotion/promotion",
  40. "iconPath": "/static/tabIcon/icon5.png",
  41. "selectedIconPath": "/static/tabIcon/icon6.png",
  42. "text": "推广圈"
  43. },
  44. {
  45. "pagePath": "pages/my/my",
  46. "iconPath": "/static/tabIcon/icon7.png",
  47. "selectedIconPath": "/static/tabIcon/icon8.png",
  48. "text": "我的"
  49. }
  50. ]
  51. },
  52. }
  53. },
  54. methods: {
  55. switchTab(item, index) {
  56. let url = '/' + item.pagePath
  57. let pagePath = url
  58. uni.switchTab({url})
  59. this.tabBar.list.forEach((v, i) => {
  60. if (item.pagePath === v.pagePath) {
  61. uni.setStorageSync('selectedIndex', index);
  62. }
  63. })
  64. }
  65. },
  66. }
  67. </script>
  68. <style>
  69. .tabbar {
  70. position: fixed;
  71. bottom: 0;
  72. left: 0;
  73. width: 100%;
  74. height: 100rpx;
  75. z-index: 999;
  76. background: #F5F5F5;
  77. border-top: 2rpx solid #eee;
  78. }
  79. .navigator {
  80. width: 85%;
  81. margin: 0 auto;
  82. padding: 20rpx;
  83. overflow: hidden;
  84. }
  85. .warpper {
  86. display: flex;
  87. justify-content: space-between;
  88. width: auto;
  89. transition-timing-function: ease-out;
  90. }
  91. .navigator-item {
  92. display: flex;
  93. align-items: center;
  94. flex-direction: column;
  95. width: 50rpx;
  96. height: 100%;
  97. }
  98. .item-text {
  99. margin-top: 6rpx;
  100. color: #777E86;
  101. font-size: 24rpx;
  102. }
  103. .text-active {
  104. color: #2E92FD !important;
  105. }
  106. .icon {
  107. width: 20px;
  108. height: 20px;
  109. }
  110. </style>

main.js注入组件

  1. // main.js
  2. import tabBar from 'components/tab-bar/tabBar.vue'
  3. Vue.component('tab-bar',tabBar) //挂载

在每一个页面中引入

page/index.vue 首页

  1. <template>
  2. <view>
  3. <view class="">我是首页</view>
  4. <button @click="toMY">跳转</button>
  5. <tab-bar ref="tabBar"></tab-bar>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. data() {
  11. return {}
  12. },
  13. methods: {
  14. toMY() {
  15. this.$refs.tabBar.switchTab({
  16. pagePath: 'pages/days/days'
  17. })
  18. uni.setStorageSync('selectedIndex', 5);
  19. }
  20. }
  21. }
  22. </script>
  1. <template>
  2. <view>
  3. <view class="">我是推广圈页</view>
  4. <tab-bar></tab-bar>
  5. </view>
  6. </template>
  1. <template>
  2. <view>
  3. <view class="">我是团队页</view>
  4. <tab-bar></tab-bar>
  5. </view>
  6. </template>

七、可拖拽式

当tabBar的数量比较多,希望可以进行拖拽,然后点击的某项时,希望他出现在视口中,那么我们可以改造一下代码。

首先调整一下css

  1. .navigator {
  2. width: 85%;
  3. margin: 0 auto;
  4. padding: 20rpx;
  5. overflow: hidden; /* 超出部分隐藏 */
  6. }
  7. .warpper {
  8. display: flex;
  9. justify-content: flex-start;
  10. width: auto; /* 自适应 */
  11. overflow-x: scroll;
  12. }
  13. .navigator-item {
  14. display: flex;
  15. align-items: center;
  16. flex-direction: column;
  17. min-width: 190rpx; /* 设置每个的宽度 */
  18. height: 100%;
  19. flex-shrink: 0 /* flex布局,关键 */
  20. }

可以看到,已实现拖拽功能,接下来就要实现点击的时候,点击项出现在视口中。

首先在computed中定义一个偏移(translateX)和一个样式(style),注:这个偏移需要在父组件中传值进去,因此需要一个offset

  1. props: {
  2. offset: {
  3. type: Number,
  4. default: 0
  5. }
  6. },
  7. computed: {
  8. style() {
  9. let style = {}
  10. style.transform = `translateX(${this.translateX}rpx)`
  11. return style
  12. },
  13. translateX: {
  14. get(){
  15. return this.offset
  16. },
  17. set(val){
  18. this.$emit('changeOffset',val)
  19. return val
  20. }
  21. }
  22. },

 页面中绑定style 

    <view ref='warpper' class="warpper" :style="style">

父组件传入值,而且是每个应用了tabBar的页面都需要写上,所以在这里使用到了mixins

  1. <template>
  2. <view>
  3. <view class="">我是团队页</view>
  4. <tab-bar :offset='offset' @changeOffset='changeOffset'></tab-bar>
  5. </view>
  6. </template>
  7. <script>
  8. import tabbarMixins from '@/mixins/tabbarMixins.js'
  9. export default {
  10. mixins:[tabbarMixins],
  11. data() {
  12. return {}
  13. },
  14. methods: {
  15. }
  16. }
  17. </script>
  1. <template>
  2. <view>
  3. <view class="">我是other</view>
  4. <tab-bar :offset='offset' @changeOffset='changeOffset'></tab-bar>
  5. </view>
  6. </template>
  7. <script>
  8. import tabbarMixins from '@/mixins/tabbarMixins.js'
  9. export default {
  10. mixins:[tabbarMixins],
  11. data() {
  12. return {
  13. }
  14. },
  15. methods: {
  16. }
  17. }
  18. </script>

 tabbarMixins中写公共方法

  1. export default {
  2. data() {
  3. return {
  4. offset:0
  5. }
  6. },
  7. onShow() {
  8. const index = uni.getStorageSync('selectedIndex')
  9. this.offset = -95 * index // -95是一个项的宽度,这里可暂时写死
  10. },
  11. methods:{
  12. // 用于实时监控偏移值
  13. changeOffset(val){
  14. this.offset = val
  15. }
  16. }
  17. }

当左右拖拽时添加一些渡效果,在warpper中绑定touch方法 

  1. <view ref='warpper' class="warpper" @touchstart="touchstart" @touchmove="touchmove"
  2. @touchend="touchend" :style="style">

添加位置信息

  1. data() {
  2. return {
  3. time: 0,
  4. startPosition: 0,
  5. offsetPosition: 0,
  6. clientX: 0,
  7. transitionDuration: 0,
  8. }
  9. },
  10. computed: {
  11. style() {
  12. let style = {}
  13. style.transform = `translateX(${this.translateX}rpx)`
  14. style.transitionDuration = this.transitionDuration + 'ms'
  15. return style
  16. }

 编写touch方法

  1. touchstart(e) {
  2. this.time = new Date() // 保存一下开始拖拽时的时间
  3. const warpper = this.$refs.warpper.$el.getBoundingClientRect()
  4. const offsetLeft = e.target.offsetLeft
  5. this.clientX = e.changedTouches[0].clientX
  6. this.startPosition = this.clientX - this.translateX // 保存当前偏移了多少位置
  7. },
  8. touchmove(e) {
  9. const clientX = e.changedTouches[0].clientX
  10. this.transitionDuration = 0
  11. this.translateX = clientX - this.startPosition // 实时改变位置
  12. },
  13. touchend(e) {
  14. const currentTime = new Date()
  15. const clientX = e.changedTouches[0].clientX
  16. const width = this.$refs.navItem[0].$el.offsetWidth
  17. const length = this.tabBar.list.length
  18. const maxOffset = width * length / 2 - width / 2 // 最大偏移量
  19. const lestOffset = width * (length - 1)
  20. const changeTime = currentTime - this.time
  21. const distance = Math.abs(clientX) - Math.abs(this.clientX)
  22. // 当按下的时间超过200ms,同时位置发生偏移大于20时,可判定为正在拖拽
  23. if (changeTime > 200 && Math.abs(distance) > 20) {
  24. this.translateX += 20 // 停止拖拽时,添加一点点阈值
  25. if (this.translateX < 0 && Math.abs(this.translateX) > lestOffset) {
  26. this.translateX = -maxOffset * 2 // 已经到最后的时候,放开手指,反弹
  27. }
  28. if (this.translateX > 0) {
  29. this.translateX = 0
  30. }
  31. this.transitionDuration = 300
  32. setTimeout(() => {
  33. this.transitionDuration = 0 // 用于实现,过渡效果
  34. }, 300)
  35. }
  36. },

以上就是实现可拖拽的实现方式,当然有一个缺点就是,点击的时候无法实现过渡效果,具体原因还未知

相关代码

  1. <template>
  2. <uni-transition mode-class="fade" :duration="200" :show="true">
  3. <view>
  4. <view class="tab-content">
  5. <slot />
  6. </view>
  7. <view class="tabbar">
  8. <view class="navigator">
  9. <view ref='warpper' class="warpper" @touchstart="touchstart" @touchmove="touchmove"
  10. @touchend="touchend" :style="style">
  11. <view ref="navItem" class="navigator-item" v-for="(item,index) in tabBar.list"
  12. :key="item.pagePath" @click="switchTab(item,index)" :data-index='index'>
  13. <img :src="item.iconPath" class="icon" v-if="selectedIndex !== index">
  14. <img :src="item.selectedIconPath" class="icon" v-else>
  15. <text :class="['item-text',{'text-active':selectedIndex === index}]">{{item.text}}</text>
  16. </view>
  17. </view>
  18. </view>
  19. </view>
  20. </view>
  21. </uni-transition>
  22. </template>
  23. <script>
  24. export default {
  25. props: {
  26. offset: {
  27. type: Number,
  28. default: 0
  29. }
  30. },
  31. data() {
  32. return {
  33. time: 0,
  34. startPosition: 0,
  35. clientX: 0,
  36. transitionDuration: 0,
  37. selectedIndex: uni.getStorageSync('selectedIndex') || 0,
  38. tabBar: {
  39. list: [{
  40. "pagePath": "pages/index/index",
  41. "iconPath": "/static/tabIcon/icon1.png",
  42. "selectedIconPath": "/static/tabIcon/icon2.png",
  43. "text": "首页"
  44. }, {
  45. "pagePath": "pages/team/team",
  46. "iconPath": "/static/tabIcon/icon3.png",
  47. "selectedIconPath": "/static/tabIcon/icon4.png",
  48. "text": "团队"
  49. }, {
  50. "pagePath": "pages/promotion/promotion",
  51. "iconPath": "/static/tabIcon/icon5.png",
  52. "selectedIconPath": "/static/tabIcon/icon6.png",
  53. "text": "推广圈"
  54. },
  55. {
  56. "pagePath": "pages/my/my",
  57. "iconPath": "/static/tabIcon/icon7.png",
  58. "selectedIconPath": "/static/tabIcon/icon8.png",
  59. "text": "我的"
  60. },
  61. {
  62. "pagePath": "pages/other/other",
  63. "iconPath": "/static/tabIcon/icon1.png",
  64. "selectedIconPath": "/static/tabIcon/icon2.png",
  65. "text": "其他"
  66. },
  67. {
  68. "pagePath": "pages/days/days",
  69. "iconPath": "/static/tabIcon/icon3.png",
  70. "selectedIconPath": "/static/tabIcon/icon4.png",
  71. "text": "日历"
  72. }
  73. ]
  74. },
  75. }
  76. },
  77. computed: {
  78. style() {
  79. let style = {}
  80. style.transform = `translateX(${this.translateX}rpx)`
  81. style.transitionDuration = this.transitionDuration + 'ms'
  82. return style
  83. },
  84. translateX: {
  85. get(){
  86. return this.offset
  87. },
  88. set(val){
  89. this.$emit('changeOffset',val)
  90. return val
  91. }
  92. }
  93. },
  94. methods: {
  95. touchstart(e) {
  96. this.time = new Date()
  97. const warpper = this.$refs.warpper.$el.getBoundingClientRect()
  98. const offsetLeft = e.target.offsetLeft
  99. this.clientX = e.changedTouches[0].clientX
  100. this.startPosition = this.clientX - this.translateX
  101. },
  102. touchmove(e) {
  103. const clientX = e.changedTouches[0].clientX
  104. this.transitionDuration = 0
  105. this.translateX = clientX - this.startPosition
  106. },
  107. touchend(e) {
  108. const currentTime = new Date()
  109. const clientX = e.changedTouches[0].clientX
  110. const width = this.$refs.navItem[0].$el.offsetWidth
  111. const length = this.tabBar.list.length
  112. const maxOffset = width * length / 2 - width / 2
  113. const lestOffset = width * (length - 1)
  114. const changeTime = currentTime - this.time
  115. const distance = Math.abs(clientX) - Math.abs(this.clientX)
  116. if (changeTime > 200 && Math.abs(distance) > 20) {
  117. this.translateX += 20
  118. if (this.translateX < 0 && Math.abs(this.translateX) > lestOffset) {
  119. this.translateX = -maxOffset * 2
  120. }
  121. if (this.translateX > 0) {
  122. this.translateX = 0
  123. }
  124. this.transitionDuration = 300
  125. setTimeout(() => {
  126. this.transitionDuration = 0
  127. }, 300)
  128. }
  129. },
  130. /*
  131. // 调整前
  132. methods: {
  133. switchTab(index, item) {
  134. let url = '/' + item.pagePath
  135. let pagePath = url
  136. const detail = {
  137. index,
  138. pagePath
  139. }
  140. if (this.$route.path !== url) {
  141. // this.__path__ = this.$route.path
  142. uni.switchTab({
  143. from: 'tabBar',
  144. url,
  145. detail
  146. })
  147. } else {
  148. // UniServiceJSBridge.emit('onTabItemTap', detail)
  149. }
  150. this.selectedIndex = index
  151. // this.$emit('switchTab', detail)
  152. }
  153. }, */
  154. // 调整后,1、调换入参顺序,方便传值,2、循环判断一下index,
  155. switchTab(item, index) {
  156. const navItem = this.$refs.navItem
  157. let url = '/' + item.pagePath
  158. let pagePath = url
  159. uni.switchTab({
  160. url
  161. })
  162. this.tabBar.list.forEach((v, i) => {
  163. if (item.pagePath === v.pagePath) {
  164. uni.setStorageSync('selectedIndex', index);
  165. }
  166. })
  167. }
  168. },
  169. }
  170. </script>
  171. <style>
  172. .tabbar {
  173. position: fixed;
  174. bottom: 0;
  175. left: 0;
  176. width: 100%;
  177. height: 100rpx;
  178. z-index: 999;
  179. background: #F5F5F5;
  180. border-top: 2rpx solid #eee;
  181. }
  182. .navigator {
  183. width: 85%;
  184. margin: 0 auto;
  185. padding: 20rpx;
  186. overflow: hidden;
  187. }
  188. .warpper {
  189. display: flex;
  190. justify-content: flex-start;
  191. width: auto;
  192. transition-timing-function: ease-out;
  193. transition-duration: 300ms;
  194. }
  195. .navigator-item {
  196. display: flex;
  197. align-items: center;
  198. flex-direction: column;
  199. min-width: 190rpx;
  200. height: 100%;
  201. flex-shrink: 0
  202. }
  203. .item-text {
  204. margin-top: 6rpx;
  205. color: #777E86;
  206. font-size: 24rpx;
  207. }
  208. .text-active {
  209. color: #2E92FD !important;
  210. }
  211. .icon {
  212. width: 20px;
  213. height: 20px;
  214. }
  215. </style>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/245799
推荐阅读
相关标签
  

闽ICP备14008679号