index.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { postMessage } from '@/helpers/native-message';
  2. import { browser } from '@/helpers/utils';
  3. import { NavBar } from 'vant';
  4. import { defineComponent, onMounted, reactive, watch } from 'vue';
  5. import { useRoute, useRouter } from 'vue-router';
  6. import styles from './index.module.less';
  7. import { state } from '@/state';
  8. export default defineComponent({
  9. name: 'm-header',
  10. props: {
  11. title: {
  12. type: String,
  13. default: ''
  14. },
  15. isBack: {
  16. type: Boolean,
  17. default: true
  18. },
  19. border: {
  20. type: Boolean,
  21. default: false
  22. },
  23. isFixed: {
  24. type: Boolean,
  25. default: true
  26. },
  27. styleName: {
  28. type: Object,
  29. default: () => ({})
  30. },
  31. background: {
  32. type: String,
  33. default: 'white'
  34. },
  35. color: {
  36. type: String,
  37. default: '#323233'
  38. },
  39. rightText: {
  40. type: String,
  41. default: ''
  42. },
  43. leftClickDefault: {
  44. type: Boolean,
  45. default: true
  46. }
  47. },
  48. emits: ["leftClick", 'rightClick'],
  49. setup(props, { emit, slots }) {
  50. const route = useRoute();
  51. const router = useRouter();
  52. const forms = reactive({
  53. title: '',
  54. navBarHeight: state.navBarHeight // 顶部高度
  55. });
  56. const onClickLeft = () => {
  57. emit('leftClick');
  58. if(props.leftClickDefault) {
  59. if (browser().isApp) {
  60. postMessage({
  61. api: 'goBack'
  62. });
  63. } else {
  64. router.back();
  65. }
  66. }
  67. };
  68. const onClickRight = () => {
  69. emit('rightClick');
  70. };
  71. onMounted(() => {
  72. forms.title = props.title || (route.meta.title as string);
  73. forms.navBarHeight = state.navBarHeight;
  74. });
  75. watch(
  76. () => props.title,
  77. () => {
  78. forms.title = props.title || (route.meta.title as string);
  79. }
  80. );
  81. return () => (
  82. <>
  83. {slots.content ? (
  84. <div
  85. style={{
  86. paddingTop: `${forms.navBarHeight}px`,
  87. background: props.background
  88. }}
  89. class={styles.headerSection}>
  90. {slots.content(forms.navBarHeight)}
  91. </div>
  92. ) : (
  93. <>
  94. <div
  95. style={{
  96. minHeight: `calc(var(--van-nav-bar-height) + ${forms.navBarHeight}px)`
  97. }}
  98. class={styles.headerSection}>
  99. <NavBar
  100. title={forms.title}
  101. class={[styles.colHeader]}
  102. style={{
  103. background: props.background,
  104. color: props.color,
  105. paddingTop: `${forms.navBarHeight}px`
  106. }}
  107. left-arrow={props.isBack}
  108. rightText={props.rightText}
  109. fixed={props.isFixed}
  110. zIndex={2000}
  111. border={props.border}
  112. onClickLeft={onClickLeft}
  113. onClickRight={onClickRight}
  114. v-slots={{
  115. right: () =>
  116. (slots.right && slots.right()) || props.rightText,
  117. title: () => (slots.title && slots.title()) || forms.title
  118. }}></NavBar>
  119. </div>
  120. {slots.default ? slots.default() : null}
  121. </>
  122. )}
  123. </>
  124. );
  125. }
  126. });