当前位置:   article > 正文

uniapp自定义导航栏样式_data-cur uni

data-cur uni

1.思路:将导航栏和导航页面分别做成组件,通过PageCur确定当前页面。

2.坑:导航页面不能使用页面生命周期,即onLoad和onShow,但是可以监听showCount去更新页面,实现onShow的功能。

  1. <template>
  2. <view class="content">
  3. <!-- 工作台 -->
  4. <staging v-if="PageCur=='home'" :isShow="PageCur=='home'" :showCount="showCount" />
  5. <!-- 应用 -->
  6. <application v-if="PageCur=='application'" :isShow="PageCur=='application'" :showCount="showCount" />
  7. <!-- 设备 -->
  8. <device v-if="PageCur=='device'" :isShow="PageCur=='device'" :showCount="showCount" />
  9. <!-- wode -->
  10. <mine v-if="PageCur=='mine'" :isShow="PageCur=='mine'" :showCount="showCount" />
  11. <view class="cu-bar tabbar bg-white shadow foot">
  12. <!-- 工作台tab -->
  13. <view :class="PageCur=='home'?'action text-primary':'action text-gray'" @click="NavChange" data-cur="home">
  14. <template v-if="PageCur=='home'">
  15. <image class="tabbar-image" mode="aspectFit" :src="homeActive"></image>
  16. </template>
  17. <template v-else>
  18. <image class="tabbar-image" mode="aspectFit" :src="homeDefault"></image>
  19. </template>
  20. 工作台
  21. </view>
  22. <!-- 应用tab -->
  23. <view :class="PageCur=='application'?'action text-primary':'action text-gray'" @click="NavChange" data-cur="application">
  24. <template v-if="PageCur=='application'">
  25. <image class="tabbar-image" mode="aspectFit" :src="workActive"></image>
  26. </template>
  27. <template v-else>
  28. <image class="tabbar-image" mode="aspectFit" :src="workDefault"></image>
  29. </template>
  30. 应用
  31. </view>
  32. <!-- 设备tab -->
  33. <view :class="PageCur=='device'?'action text-primary':'action text-gray'" @click="NavChange" data-cur="device">
  34. <template v-if="PageCur=='device'">
  35. <image class="tabbar-image" mode="aspectFit" :src="deviceActive"></image>
  36. </template>
  37. <template v-else>
  38. <image class="tabbar-image" mode="aspectFit" :src="deviceDefault"></image>
  39. </template>
  40. 设备
  41. </view>
  42. <view :class="PageCur=='mine'?'action text-primary':'action text-gray'" @click="NavChange" data-cur="mine">
  43. <template v-if="PageCur=='mine'">
  44. <image class="tabbar-image" mode="aspectFit" :src="mineActive"></image>
  45. </template>
  46. <template v-else>
  47. <image class="tabbar-image" mode="aspectFit" :src="mineDefault"></image>
  48. </template>
  49. 我的
  50. </view>
  51. </view>
  52. </view>
  53. </template>
  54. <script>
  55. import { checkLoginTimeout } from '@/utils/auth.js';
  56. import homeActive from '@/static/images/tabbar/home_.png';
  57. import homeDefault from '@/static/images/tabbar/home.png';
  58. import workActive from '@/static/images/tabbar/work_.png';
  59. import workDefault from '@/static/images/tabbar/work.png';
  60. import deviceActive from '@/static/images/tabbar/device_.png';
  61. import deviceDefault from '@/static/images/tabbar/device.png';
  62. import mineActive from '@/static/images/tabbar/mine_.png';
  63. import mineDefault from '@/static/images/tabbar/mine.png';
  64. // 工作台
  65. import staging from './staging/index.vue';
  66. // 应用
  67. import application from './application/index.vue';
  68. // 设备
  69. import device from './device/index.vue';
  70. // 我的
  71. import mine from './mine/index.vue';
  72. export default {
  73. components: {
  74. staging,
  75. application,
  76. device,
  77. mine
  78. },
  79. data() {
  80. return {
  81. PageCur: 'home',
  82. showCount: null,
  83. homeActive,
  84. homeDefault,
  85. workActive,
  86. workDefault,
  87. deviceActive,
  88. deviceDefault,
  89. mineActive,
  90. mineDefault,
  91. }
  92. },
  93. onLoad: function(option) {
  94. console.log('onLoad')
  95. this.PageCur = option?.page || 'home';
  96. },
  97. onShow() {
  98. console.log('onShow')
  99. checkLoginTimeout();
  100. this.showCount = this.showCount === null ? 0 : this.showCount + 1;
  101. },
  102. methods: {
  103. NavChange: function(e) {
  104. this.showCount = 0;
  105. this.PageCur = e.currentTarget.dataset.cur;
  106. },
  107. }
  108. }
  109. </script>
  110. <style>
  111. .tabbar-image {
  112. height: 72upx;
  113. width: 72upx;
  114. display: block;
  115. margin: auto;
  116. }
  117. </style>
  118. //在staging 里,接收showCount,并watch它
  119. props:{
  120. showCount:{
  121. typeof:[String,Number],
  122. }
  123. },
  124. watch:{
  125. showCount(){
  126. this.getRecord()
  127. }
  128. }

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

闽ICP备14008679号