index.tsx 2.9 KB

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