ut-navbar.vue 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <template>
  2. <view>
  3. <up-navbar :fixed="fixed" :border="border">
  4. <template #left>
  5. <slot name="left">
  6. <view class="d-flex a-c">
  7. <up-icon v-if="showBack" name="arrow-left" size="40rpx" color="#333" @click="handleBackClick"></up-icon>
  8. <up-icon v-else name="home" size="40rpx" color="#333" @click="handleHomeClick"></up-icon>
  9. <slot name="leftText">
  10. <text class="f-s-38 c-#333 f-w-500 ml-20">{{ leftText }}</text>
  11. </slot>
  12. </view>
  13. </slot>
  14. </template>
  15. <template #center>
  16. <slot name="center">
  17. <text class="f-s-38 c-#333 f-w-500">{{ title }}</text>
  18. </slot>
  19. </template>
  20. </up-navbar>
  21. <view v-if="breadcrumb" class="bg-#EBF6EE pd2-10-24 f-s-24 c-#3FAD5B">
  22. <slot name="breadcrumb">
  23. <template v-for="(item, index) in breads" :key="index">{{ index ? ' > ' : '' }}{{ item.title }}</template>
  24. </slot>
  25. </view>
  26. </view>
  27. </template>
  28. <script lang="ts" setup>
  29. import { usePagesStore } from '@/store/modules/pages';
  30. import { ref, onMounted } from 'vue';
  31. const pagesStore = usePagesStore();
  32. const props = withDefaults(
  33. defineProps<{
  34. title?: string;
  35. border?: boolean;
  36. fixed?: boolean;
  37. homeUrl?: string;
  38. leftText?: string;
  39. // 是否有面包屑
  40. breadcrumb?: boolean;
  41. }>(),
  42. {
  43. title: '',
  44. border: true,
  45. fixed: true,
  46. homeUrl: '/pages/index/index',
  47. leftText: '',
  48. breadcrumb: true,
  49. }
  50. );
  51. const emit = defineEmits<{
  52. (e: 'back'): void;
  53. (e: 'home'): void;
  54. }>();
  55. const showBack = ref(false);
  56. onMounted(() => {
  57. const pages = typeof getCurrentPages === 'function' ? getCurrentPages() : [];
  58. showBack.value = pages.length > 1;
  59. });
  60. function handleBackClick() {
  61. emit('back');
  62. const pages = typeof getCurrentPages === 'function' ? getCurrentPages() : [];
  63. if (pages.length > 1) {
  64. uni.navigateBack();
  65. } else {
  66. uni.switchTab({ url: props.homeUrl });
  67. }
  68. }
  69. function handleHomeClick() {
  70. emit('home');
  71. uni.switchTab({ url: props.homeUrl });
  72. }
  73. const breads = ref<any[]>([])
  74. const initPages = () => {
  75. const pages = getCurrentPages()
  76. // 获取当前页面
  77. const currentPage: any = pages[pages.length - 1]
  78. if (props.title || props.leftText) {
  79. pagesStore.setPageTitle(currentPage.route, props.title || props.leftText);
  80. }
  81. breads.value = pages.map((item: any) => {
  82. const route = item.route;
  83. return {
  84. title: pagesStore.mapPages.get(route)?.title || '未命名页面',
  85. route,
  86. }
  87. })
  88. };
  89. onMounted(() => {
  90. initPages();
  91. });
  92. </script>