当前位置:   article > 正文

uniapp 微信小程序使用uview u-tabbar组件自定义tabbar

u-tabbar

1.在components文件下面新建TabBar.vue组件, 使用uview的u-tabbar组件进行二次封装; u-tabbar组件中value取当前匹配项的name, 一般从父组件传过来即可; 在u-tabbar-item标签内可以使用插槽 slot='inactive-icon'(选中的图标)和slot='inactive-icon'(未选中的图标)自定义图片样式

u-tabbar组件默认已经为iPhoneX等机型留出底部安全距离, 具体可以看uview官方文档

  1. <template>
  2. <view>
  3. <u-tabbar
  4. :value="currentTab"
  5. activeColor="#2D73F0"
  6. inactiveColor="#333"
  7. >
  8. <u-tabbar-item
  9. v-for="(item,i) in tabList"
  10. :key="item.name"
  11. :text="item.text"
  12. :name="item.name"
  13. @click="handTab(item)"
  14. >
  15. <image class="bar_img" slot='active-icon' :src="item.selectedIconPath"></image>
  16. <image class="bar_img" slot='inactive-icon' :src="item.iconPath"></image>
  17. </u-tabbar-item>
  18. </u-tabbar>
  19. </view>
  20. </template>
  21. <script>
  22. export default {
  23. name:"TabBar",
  24. props: {
  25. currentTab: {
  26. type: String,
  27. default: 'home'
  28. }
  29. },
  30. data() {
  31. return {
  32. tabList: [
  33. {
  34. "pagePath": "/pages/home",
  35. "iconPath": "/static/home.png",
  36. "selectedIconPath": "/static/home_sec.png",
  37. "text": "首页",
  38. "name": 'home',
  39. },
  40. {
  41. "pagePath": "/pages/user",
  42. "iconPath": "/static/user.png",
  43. "selectedIconPath": "/static/user_sec.png",
  44. "text": "我的",
  45. "name": "user"
  46. }
  47. ]
  48. }
  49. },
  50. methods: {
  51. handTab(row) {
  52. uni.switchTab({
  53. url: row.pagePath
  54. })
  55. }
  56. }
  57. }
  58. </script>
  59. <style lang="scss" scoped>
  60. .bar_img{
  61. width: 54rpx;
  62. height: 54rpx;
  63. }
  64. </style>

2.pages.json里面配置tabBar里面的custom为true

3.在home和user页面引入自定义的组件TabBar, 这样就完成基本的自定义tabbar, 根据不同的需求可以修改里面的逻辑, 比如不同角色进来的tabbar不一样, 个别tabbar没有文字就显示一个大图等等.

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

闽ICP备14008679号