index.vue 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <template>
  2. <view
  3. class="tabs-view"
  4. :style="{
  5. '--active-line-color': activeLineColor,
  6. '--active-line-height': activeLineHeight,
  7. '--active-line-width': activeLineWidth,
  8. }"
  9. >
  10. <view
  11. class="tabs-item"
  12. v-for="item in tabsList"
  13. :key="item.name"
  14. @click="$emit('tab-click', item)"
  15. >
  16. <text :class="{ active: item.name === activeName }">
  17. {{ item.name }}</text
  18. >
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. export default {
  24. name: "Tabs",
  25. // 接收父组件传递的参数
  26. props: {
  27. // 标签列表数据
  28. tabsList: {
  29. type: Array,
  30. required: true,
  31. validator: (value) => {
  32. // 验证每个标签项是否包含name属性
  33. return value.every((item) => item.hasOwnProperty("name"));
  34. },
  35. },
  36. // 当前激活的标签名
  37. activeName: {
  38. type: String,
  39. required: true,
  40. },
  41. // 激活线颜色
  42. activeLineColor: {
  43. type: String,
  44. default: "#f8c20f", // 默认颜色
  45. },
  46. // 激活线高度
  47. activeLineHeight: {
  48. type: String,
  49. default: "4rpx", // 默认高度
  50. },
  51. // 激活线宽度
  52. activeLineWidth: {
  53. type: String,
  54. default: "50rpx", // 默认宽度
  55. },
  56. },
  57. };
  58. </script>
  59. <style lang="scss" scoped>
  60. .tabs-view {
  61. display: flex;
  62. align-items: center;
  63. width: 100%;
  64. .tabs-item {
  65. flex: 1;
  66. display: flex;
  67. justify-content: center;
  68. align-items: center;
  69. position: relative;
  70. text {
  71. &.active {
  72. border-bottom: 4rpx solid var(--active-line-color);
  73. }
  74. }
  75. }
  76. }
  77. </style>