cl-tabbar.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <view
  3. class="cl-tabbar"
  4. :style="{
  5. height,
  6. backgroundColor,
  7. borderTopColor: borderStyle,
  8. }"
  9. >
  10. <slot></slot>
  11. </view>
  12. </template>
  13. <script>
  14. import { parseRpx, isEmpty } from "../../utils";
  15. /**
  16. * tabbar 底部导航
  17. * @description 该组件与原生tabbar配置一致,同时添加新的支持。
  18. * @tutorial https://docs.cool-js.com/uni/components/nav/tabbar.html
  19. * @property {String} value 绑定值,页面标识
  20. * @property {String} height 高度
  21. * @property {String} color 字体颜色,默认#7A7E83
  22. * @property {String} selectedColor 选中颜色,默认#2B2E3D
  23. * @property {String} borderStyle 边框颜色,默认white
  24. * @property {String} backgroundColor 背景颜色,默认#ffffff
  25. * @property {Function} beforeSwitch 切换前钩子函数
  26. * @example 见教程
  27. */
  28. export default {
  29. name: "cl-tabbar",
  30. componentName: "ClTabbar",
  31. props: {
  32. // 绑定值,页面标识
  33. value: [String, Number],
  34. // 高度
  35. height: {
  36. type: String,
  37. default: "100rpx",
  38. },
  39. // 字体颜色
  40. color: {
  41. type: String,
  42. default: "#7A7E83",
  43. },
  44. // 选中颜色
  45. selectedColor: {
  46. type: String,
  47. default: "#2B2E3D",
  48. },
  49. // 边框颜色
  50. borderStyle: {
  51. type: String,
  52. default: "white",
  53. },
  54. // 背景颜色
  55. backgroundColor: {
  56. type: String,
  57. default: "#ffffff",
  58. },
  59. // 切换前钩子函数
  60. beforeSwitch: Function,
  61. },
  62. data() {
  63. return {
  64. name: null,
  65. lock: false,
  66. number: 4,
  67. };
  68. },
  69. watch: {
  70. value: {
  71. immediate: true,
  72. handler(val) {
  73. this.name = val;
  74. },
  75. },
  76. name(val) {
  77. this.$emit("input", val);
  78. },
  79. },
  80. created() {
  81. this.$on("change", (name) => {
  82. if (!this.lock) {
  83. let next = (n2) => {
  84. this.name = n2 || name;
  85. this.lock = false;
  86. };
  87. if (this.beforeSwitch) {
  88. this.lock = true;
  89. this.beforeSwitch(name, next);
  90. } else {
  91. next(name);
  92. }
  93. }
  94. });
  95. },
  96. mounted() {
  97. this.doLayout();
  98. },
  99. methods: {
  100. doLayout() {
  101. let timer = null;
  102. const fn = () => {
  103. if (isEmpty(this.$children)) {
  104. timer = setTimeout(() => {
  105. fn();
  106. }, 50);
  107. } else {
  108. clearTimeout(timer);
  109. this.number = this.$children.length;
  110. }
  111. };
  112. fn();
  113. },
  114. },
  115. };
  116. </script>