GoodsNav.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <view class="goods-nav" v-if="navList && navList.length > 0">
  3. <view class="nav-row">
  4. <view class="nav-all" :class="{ active: activeId === 'all' || activeId === 0 || activeId === '0' }" @tap.stop="onSelectAll" @click.stop="onSelectAll">
  5. <text>全部</text>
  6. </view>
  7. <scroll-view scroll-x class="nav-scroll" :show-scrollbar="false">
  8. <view class="nav-inner">
  9. <view
  10. v-for="(item, index) in navList"
  11. :key="item.id || item.value || index"
  12. :class="['nav-item', { active: (item.id || item.value) === activeId }]"
  13. @tap.stop="onSelectByIndex(index)"
  14. @click.stop="onSelectByIndex(index)"
  15. >
  16. <text>{{ item.name || item.categoryName || item.label }}</text>
  17. </view>
  18. </view>
  19. </scroll-view>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. export default {
  25. name: 'GoodsNav',
  26. props: {
  27. navList: { type: Array, default: () => [] },
  28. activeId: { type: [String, Number], default: null }
  29. },
  30. methods: {
  31. onSelectAll() {
  32. this.$emit('select', { id: 'all', name: '全部', categoryName: '全部' });
  33. },
  34. onSelectByIndex(index) {
  35. const list = this.navList;
  36. if (!list || index < 0 || index >= list.length) {
  37. console.warn('[GoodsNav] onSelectByIndex 无效 index:', index, 'list.length:', list ? list.length : 0);
  38. return;
  39. }
  40. const item = list[index];
  41. this.$emit('select', item);
  42. }
  43. }
  44. };
  45. </script>
  46. <style lang="scss" scoped>
  47. .goods-nav {
  48. width: 100%;
  49. }
  50. .nav-row {
  51. display: flex;
  52. align-items: center;
  53. padding: 0 24rpx;
  54. gap: 30rpx;
  55. }
  56. .nav-all {
  57. flex-shrink: 0;
  58. font-family: PingFangSC, PingFang SC;
  59. font-weight: 400;
  60. font-size: 40rpx;
  61. color: rgba(0,0,0,0.85);
  62. line-height: 56rpx;
  63. }
  64. .nav-all.active {
  65. color: #FF233C;
  66. font-weight: 600;
  67. }
  68. .nav-scroll {
  69. flex: 1;
  70. }
  71. .nav-inner {
  72. display: inline-flex;
  73. padding:0;
  74. gap: 30rpx;
  75. }
  76. .nav-item {
  77. flex-shrink: 0;
  78. font-family: PingFangSC, PingFang SC;
  79. font-weight: 400;
  80. font-size: 40rpx;
  81. color: rgba(0,0,0,0.85);
  82. line-height: 56rpx;
  83. }
  84. .nav-item.active {
  85. color: #FF233C;
  86. font-weight: 600;
  87. }
  88. .nav-item.active text {
  89. // border-bottom: 2rpx solid #E5212B;
  90. // padding-bottom: 4rpx;
  91. }
  92. </style>