index.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. },
  44. emits: ['rightClick'],
  45. setup(props, { emit, slots }) {
  46. const route = useRoute();
  47. const router = useRouter();
  48. const forms = reactive({
  49. title: '',
  50. navBarHeight: state.navBarHeight // 顶部高度
  51. });
  52. const onClickLeft = () => {
  53. if (browser().isApp) {
  54. postMessage({
  55. api: 'goBack'
  56. });
  57. } else {
  58. router.back();
  59. }
  60. };
  61. const onClickRight = () => {
  62. emit('rightClick');
  63. };
  64. onMounted(() => {
  65. forms.title = props.title || (route.meta.title as string);
  66. forms.navBarHeight = state.navBarHeight;
  67. });
  68. watch(
  69. () => props.title,
  70. () => {
  71. forms.title = props.title || (route.meta.title as string);
  72. }
  73. );
  74. return () => (
  75. <>
  76. {slots.content ? (
  77. <div
  78. style={{
  79. paddingTop: `${forms.navBarHeight}px`,
  80. background: props.background
  81. }}
  82. class={styles.headerSection}>
  83. {slots.content(forms.navBarHeight)}
  84. </div>
  85. ) : (
  86. <>
  87. <div
  88. style={{
  89. minHeight: `calc(var(--van-nav-bar-height) + ${forms.navBarHeight}px)`
  90. }}
  91. class={styles.headerSection}>
  92. <NavBar
  93. title={forms.title}
  94. class={[styles.colHeader]}
  95. style={{
  96. background: props.background,
  97. color: props.color,
  98. paddingTop: `${forms.navBarHeight}px`
  99. }}
  100. left-arrow={props.isBack}
  101. rightText={props.rightText}
  102. fixed={props.isFixed}
  103. zIndex={2000}
  104. border={props.border}
  105. onClickLeft={onClickLeft}
  106. onClickRight={onClickRight}
  107. v-slots={{
  108. right: () =>
  109. (slots.right && slots.right()) || props.rightText,
  110. title: () => (slots.title && slots.title()) || forms.title
  111. }}></NavBar>
  112. </div>
  113. {slots.default ? slots.default() : null}
  114. </>
  115. )}
  116. </>
  117. );
  118. }
  119. });