MHeader.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <div class="mheader">
  3. <header class="m-nav-header" :class="[isFixed ? 'fixed' : '']">
  4. <div class="m-nav-bar__left" @click="goBack" v-show="isBack">
  5. <van-icon class="arrow-left" name="arrow-left"></van-icon>
  6. </div>
  7. <div class="m-nav-bar__title">
  8. <slot>{{ name ? name : this.$route.meta.descrition }}</slot>
  9. </div>
  10. <div class="m-nav-bar__right">
  11. <slot name="right"></slot>
  12. </div>
  13. </header>
  14. </div>
  15. </template>
  16. <script>
  17. /**
  18. * 插槽使用方式
  19. * <template v-slot:right>
  20. 这里
  21. </template>
  22. */
  23. export default {
  24. name: 'mheader',
  25. props: {
  26. name: String, // 标题名称
  27. isBack: { // 是否显示返回按钮
  28. type: Boolean,
  29. default: true
  30. },
  31. isFixed: { // 是否固定顶部
  32. type: Boolean,
  33. default: true
  34. },
  35. backUrl: { // 跳转指定的URL
  36. type: Object,
  37. default: () => {
  38. return {
  39. callBack: null, // 方法
  40. path: '', // 跳转路径
  41. params: {}, // 跳转参数 目前只能用query的方式
  42. }
  43. }
  44. }
  45. },
  46. methods: {
  47. goBack() { // 返回上层
  48. let urlObj = this.backUrl
  49. // console.log(typeof urlObj.callBack)
  50. if(typeof urlObj.callBack == 'function') {
  51. urlObj.callBack()
  52. } else {
  53. if(urlObj.path) {
  54. this.$router.push({
  55. path: urlObj.path,
  56. query: urlObj.params
  57. })
  58. } else {
  59. history.go(-1)
  60. }
  61. }
  62. }
  63. }
  64. }
  65. </script>
  66. <style lang="less" scoped>
  67. @import url('../assets/commonLess/variable');
  68. .mheader {
  69. height: .46rem;
  70. overflow: hidden;
  71. }
  72. .m-nav-header {
  73. position: absolute;
  74. left: 0;
  75. top: 0;
  76. width: 100%;
  77. height: .46rem;
  78. line-height: .46rem;
  79. background-color: @whiteColor;
  80. text-align: center;
  81. user-select: none;
  82. color: @blackColor;
  83. &.fixed {
  84. position: fixed;
  85. z-index: 99;
  86. }
  87. .m-nav-bar__title {
  88. max-width: 60%;
  89. margin: 0 auto;
  90. color: @blackColor;
  91. font-weight: 500;
  92. font-size: .16rem;
  93. }
  94. .m-nav-bar__left, .m-nav-bar__right {
  95. position: absolute;
  96. bottom: 0;
  97. }
  98. .m-nav-bar__left {
  99. left: .12rem;
  100. .arrow-left {
  101. font-size: .21rem;
  102. vertical-align: middle;
  103. }
  104. }
  105. .m-nav-bar__right {
  106. right: .12rem;
  107. }
  108. }
  109. </style>