custom-tabbar.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <template>
  2. <view class="custom-tabbar">
  3. <view class="tab-item" v-for="(item, index) in tabList" :key="index" @click="switchTab(item.path)">
  4. <!-- 使用图标和文本来表示每个 Tab 项 -->
  5. <image :src="item.iconPath" class="tab-icon"></image>
  6. <text>{{ item.text }}</text>
  7. </view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. tabList: [
  15. { text: '学习计划', iconPath: 'static/images/tabbar/unselect/plan-sj.png',path:'/pages/study/index' },
  16. { text: '成长', iconPath: 'static/images/tabbar/unselect/develop-sj.png',path:'/pages/study/index' },
  17. { text: '伙伴', iconPath: 'static/images/tabbar/unselect/partner-sj.png',path:'/pages/study/index' },
  18. { text: '搜索', iconPath: 'static/images/tabbar/unselect/my-sj.png',path:'/pages/my/index' },
  19. // 更多 Tab 项...
  20. ]
  21. };
  22. },
  23. methods: {
  24. switchTab(path, params = {}) {
  25. // 将参数拼接到路径中
  26. let queryString = Object.keys(params).map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`).join('&');
  27. if (queryString) {
  28. path += `?${queryString}`;
  29. }
  30. // 使用 uni.switchTab 进行跳转(适用于 tabBar 页面)
  31. if (this.isTabBarPage(path)) {
  32. uni.switchTab({
  33. url: path
  34. });
  35. } else {
  36. // 使用 uni.navigateTo 进行跳转(适用于非 tabBar 页面)
  37. uni.navigateTo({
  38. url: path
  39. });
  40. }
  41. },
  42. isTabBarPage(path) {
  43. // 根据你的 tabBar 页面路径进行判断
  44. const tabBarPages = ['/pages/study/index', '/pages/study/index','/pages/study/index','/pages/my/index'];
  45. return tabBarPages.includes(path);
  46. }
  47. }
  48. }
  49. </script>
  50. <style scoped>
  51. .custom-tabbar {
  52. display: flex;
  53. justify-content: space-around;
  54. align-items: center;
  55. /* 其他样式 */
  56. }
  57. .tab-item {
  58. flex: 1;
  59. /* 样式 */
  60. }
  61. .tab-icon {
  62. /* 图标样式 */
  63. }
  64. </style>