index.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <template>
  2. <div class="app-tabbar">
  3. <div class="app-tabbar__wrapper" :style="styles">
  4. <template v-for="(item, index) in dataList" :key="index">
  5. <div :class="['app-tabbar__item', 'app-tabbar__item--' + item.name, dataIndex === index && 'is-active']"
  6. @click="onClick(index)">
  7. <slot :item="item" :index="index">
  8. <!--判断是否图片图标-->
  9. <template v-if="item.iconType === 'image'">
  10. <div :class="['g-icon', dataIndex === index && 'active']">
  11. <img :src="item.activeIcon" v-if="dataIndex === index" />
  12. <img :src="item.icon" v-else />
  13. <span>{{ item.label }}</span>
  14. </div>
  15. </template>
  16. <template v-else>
  17. <app-iconfont label-direction="bottom" :icon="item.icon" :active-icon="item.activeIcon"
  18. :active="dataIndex === index">{{ item.label }}</app-iconfont>
  19. </template>
  20. </slot>
  21. </div>
  22. </template>
  23. </div>
  24. </div>
  25. </template>
  26. <script lang="ts" setup>
  27. import { PropType, computed } from 'vue'
  28. import { Tabbar } from './types'
  29. import { useGlobalStore } from '@/stores'
  30. import AppIconfont from '../../base/iconfont/index.vue'
  31. const emit = defineEmits(['click'])
  32. const props = defineProps({
  33. // 数据列表
  34. dataList: {
  35. type: Array as PropType<Tabbar[]>,
  36. default: () => ([]),
  37. },
  38. // 当前标签索引
  39. dataIndex: {
  40. type: Number,
  41. default: 0,
  42. },
  43. // 是否固定在底部
  44. fixed: {
  45. type: Boolean,
  46. default: false,
  47. }
  48. })
  49. const globalStore = useGlobalStore()
  50. const styles = computed(() => ({
  51. position: props.fixed ? 'fixed' : 'static',
  52. width: globalStore.clientWidth + 'px',
  53. }))
  54. const onClick = (index: number) => {
  55. if (props.dataIndex !== index) {
  56. emit('click', index)
  57. }
  58. }
  59. </script>
  60. <style lang="less" scoped>
  61. @import './index.less';
  62. </style>