index.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <template>
  2. <div
  3. class="tabbar-item"
  4. :style="{
  5. color:
  6. state.active == state.index ? state.activeColor : state.unactiveColor
  7. }"
  8. @click="change(state.index)"
  9. >
  10. <view class="icon-box">
  11. <view class="tips num" v-if="num && num <= 99">
  12. {{ num }}
  13. </view>
  14. <view class="tips nums" v-else-if="num && num > 100">{{ '99+' }}</view>
  15. <view v-if="icon">
  16. <nut-icon class="icon" :size="state.size" :name="icon"></nut-icon>
  17. </view>
  18. <view :class="['tabbar-nav-word', { 'big-word': !icon }]">{{
  19. tabTitle
  20. }}</view>
  21. </view>
  22. </div>
  23. </template>
  24. <script lang="ts">
  25. import { createComponent } from '@/utils/create';
  26. import {
  27. ComponentInternalInstance,
  28. computed,
  29. getCurrentInstance,
  30. inject,
  31. reactive,
  32. watch
  33. } from 'vue';
  34. const { create } = createComponent('tabbar-item');
  35. export default create({
  36. props: {
  37. tabTitle: {
  38. // 标签页的标题
  39. type: String,
  40. default: ''
  41. },
  42. icon: {
  43. // 标签页显示的icon
  44. type: String,
  45. default: ''
  46. },
  47. href: {
  48. // 标签页的跳转链接
  49. type: String,
  50. default: ''
  51. },
  52. num: {
  53. // 页签右上角的数字角标
  54. type: String,
  55. default: ''
  56. }
  57. },
  58. setup(props, ctx) {
  59. const parent: any = inject('parent');
  60. const state = reactive({
  61. size: parent.size,
  62. unactiveColor: parent.unactiveColor, // 未选中的颜色
  63. activeColor: parent.activeColor, // 选中的颜色
  64. active: parent.modelValue, // 是否选中
  65. index: 0
  66. });
  67. const relation = (child: ComponentInternalInstance): void => {
  68. if (child.proxy) {
  69. let index = parent.children.length;
  70. state.index = index;
  71. let obj = Object.assign({}, child.proxy, { index });
  72. parent.children.push(obj);
  73. }
  74. };
  75. relation(getCurrentInstance() as ComponentInternalInstance);
  76. function change(index: Number) {
  77. parent.changeIndex(index);
  78. }
  79. const choosed = computed(() => {
  80. if (parent) {
  81. return parent.modelValue;
  82. }
  83. return null;
  84. });
  85. watch(choosed, (value, oldValue) => {
  86. state.active = value;
  87. if (parent.children[value].href) {
  88. window.location.href = parent.children[value].href;
  89. }
  90. });
  91. return {
  92. state,
  93. change
  94. };
  95. }
  96. });
  97. </script>
  98. <style lang="scss">
  99. @import 'index.scss';
  100. </style>