当前位置:   article > 正文

Taro + Vue 自定义标签栏TabBar_taro vue自定义tabbar

taro vue自定义tabbar

、1、app.config.ts:自定义TabBar就必须将custom设为true

2、 定义TabBar组件:

 customTabBar.vue

  1. <template>
  2. <view class='bottom-tab'>
  3. <view class='bottom-tab-item' v-for="(tabItem, index) in list" :key="tabItem.key" @tap="switchTab(tabItem.pagePath, index)">
  4. <image class='bottom-tab-item-img' :src="selected == index ? tabItem.selectedIconPath : tabItem.iconPath " />
  5. <view class='bottom-tab-item-text' :style="{color:(selected==index?selectedColor:color)}" >
  6. {{tabItem.text}}
  7. </view>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. import './customTabBar.scss'
  13. import Taro from '@tarojs/taro'
  14. import commonUtil from "../../utils/util.ts"
  15. import {
  16. } from "taro-ui-vue3"
  17. export default {
  18. components: {
  19. },
  20. data () {
  21. return {
  22. selected: 0,
  23. color: 'rgba(68, 68, 68, 1)',
  24. selectedColor: 'rgba(68, 68, 68, 1)',
  25. list: [
  26. {
  27. "pagePath": "pages/tabBarPackages/home/home",
  28. "iconPath": "../../../images/foot_1.png",
  29. "selectedIconPath": "../../../images/foot_1.png",
  30. "text": "首页",
  31. },
  32. {
  33. "pagePath": "pages/tabBarPackages/userList/userList",
  34. "text": "用户",
  35. "iconPath": "../../../images/foot_2.png",
  36. "selectedIconPath": "../../../images/foot_2.png"
  37. },
  38. {
  39. "pagePath": "pages/tabBarPackages/userAdd/userAdd",
  40. "text": "",
  41. "iconPath": "../../../images/foot_3.png",
  42. "selectedIconPath": "../../../images/foot_3.png"
  43. },
  44. {
  45. "pagePath": "pages/tabBarPackages/teamList/teamList",
  46. "text": "小组",
  47. "iconPath": "../../../images/foot_4.png",
  48. "selectedIconPath": "../../../images/foot_4.png"
  49. },
  50. {
  51. "pagePath": "pages/tabBarPackages/personal/personal",
  52. "text": "我的",
  53. "iconPath": "../../../images/foot_5.png",
  54. "selectedIconPath": "../../../images/foot_5.png"
  55. }
  56. ]
  57. }
  58. },
  59. created () {
  60. },
  61. methods: {
  62. switchTab(pagePath, index){
  63. this.selected = index
  64. Taro.switchTab({
  65. url: '../../../' + pagePath
  66. })
  67. }
  68. },
  69. }
  70. </script>

customTabBar.scss(这段css样式完全copy的这篇文章https://www.jianshu.com/p/ba33d0be8f03/)

  1. .bottom-tab {
  2. position: fixed;
  3. display: flex;
  4. width: 100%;
  5. height: 49PX;
  6. background: white;
  7. box-shadow: 0PX -5PX 10PX 0PX rgba(0, 0, 0, 0.03);
  8. bottom: 0;
  9. &-item {
  10. flex: 1;
  11. display: flex;
  12. flex-direction: column;
  13. align-items: center;
  14. &-img {
  15. margin: 5PX auto 0;
  16. width: 24PX;
  17. height: 24PX;
  18. }
  19. &-text {
  20. height: 14PX;
  21. line-height: 14PX;
  22. font-size: 10PX;
  23. font-family: PingFangSC-Light, PingFang SC;
  24. font-weight: 300;
  25. color: rgba(68, 68, 68, 1);
  26. }
  27. }
  28. }

3、tab页面调用组件

例:home页面

home.config.ts

  1. export default {
  2. navigationBarTitleText: '用户管理平台',
  3. "usingComponents": {
  4. 'customTabBar': '../../../components/custom-tab-bar/customTabBar'
  5. },
  6. }

home.vue

  1. <template>
  2. ......
  3. <customTabBar></customTabBar>
  4. </template>
  5. <script>
  6. import "./home.scss";
  7. import Taro, { getCurrentInstance } from "@tarojs/taro";
  8. import { AtGrid, AtButton, AtNoticebar, AtIcon, AtToast } from "taro-ui-vue3";
  9. import commonUtil from "../../../utils/util.ts";
  10. import customTabBar from "../../../components/custom-tab-bar/customTabBar.vue";
  11. export default {
  12. components: {
  13. AtGrid,
  14. AtButton,
  15. AtNoticebar,
  16. AtIcon,
  17. AtToast,
  18. customTabBar
  19. },
  20. data() {
  21. ......
  22. };
  23. </script>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/751250
推荐阅读
  

闽ICP备14008679号