uni-NavBar.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <template>
  2. <view class="navbar dis a-c j-s" :style="{background:background,borderBottom:borderBottom}">
  3. <image class="icon-right" :src="backActionimg" mode="" @tap="back()"></image>
  4. <template v-if="!isSlot">
  5. <text class="title font-weight" :style="titleStyle">{{headerTitle}}</text>
  6. <template v-if="!rightSlot">
  7. <view class="dis f-c a-c" :style="{opacity:isShared?1:0}">
  8. <image v-if="rightIcon" class="icon-right" :src="computedRightIcon" mode="" @tap="share">
  9. </image>
  10. <text v-if="rightText" class="right-text" @tap="share" :style="rightTextStyle">{{rightText}}</text>
  11. </view>
  12. </template>
  13. <template v-else>
  14. <view class="dis a-c ">
  15. <slot name="rightSlot">
  16. </slot>
  17. </view>
  18. </template>
  19. </template>
  20. <template v-else>
  21. <slot name="search">
  22. </slot>
  23. </template>
  24. </view>
  25. </template>
  26. <script>
  27. import {
  28. turnBase64Image
  29. } from '@/plugins/utils.js';
  30. export default {
  31. name: 'name', //插件名称
  32. props: {
  33. isSlot: {
  34. type: Boolean,
  35. default: false,
  36. },
  37. rightSlot: {
  38. type: Boolean,
  39. default: false,
  40. },
  41. headerTitle: {
  42. type: String,
  43. default: 'xxxx'
  44. },
  45. background: {
  46. type: String,
  47. default: '#fff'
  48. },
  49. borderBottom: {
  50. type: String,
  51. default: 'none'
  52. },
  53. titleStyle: {
  54. type: Object,
  55. default: () => {
  56. return {};
  57. }
  58. },
  59. rightTextStyle: {
  60. type: Object,
  61. default: () => {
  62. return {};
  63. }
  64. },
  65. rightIcon: {
  66. type: Boolean,
  67. default: true,
  68. },
  69. iconImage: {
  70. type: String,
  71. default: null
  72. },
  73. rightBtnType: {
  74. type: String,
  75. default: 'icon' //icon 图标按钮 text 文字按钮
  76. },
  77. rightText: {
  78. type: String,
  79. default: "",
  80. },
  81. isShared: {
  82. type: Boolean,
  83. default: true,
  84. },
  85. backKeySlot: { //返回键插槽
  86. type: Boolean,
  87. default: false,
  88. }
  89. },
  90. computed: {
  91. },
  92. data() {
  93. return {
  94. backActionimg: '', //返回图标
  95. shareimg: '',
  96. computedRightIcon: "",
  97. };
  98. },
  99. async mounted() {
  100. this.backActionimg = await turnBase64Image('/static/icon/backAction.png')
  101. this.shareimg = await turnBase64Image('/static/icon/share.png')
  102. // 处理 rightIcon 的转换
  103. if (this.iconImage) {
  104. this.computedRightIcon = await turnBase64Image(this.iconImage);
  105. } else {
  106. this.computedRightIcon = this.shareimg; // 如果没有 iconImage,使用 shareimg
  107. }
  108. },
  109. methods: {
  110. share() {
  111. this.$emit('share')
  112. },
  113. back() {
  114. // 默认返回上一级,自定义插槽特殊处理
  115. if (this.backKeySlot) {
  116. this.$emit('backKeyChange')
  117. } else {
  118. uni.navigateBack(-1)
  119. }
  120. }
  121. }
  122. };
  123. </script>
  124. <!--使用scss,只在本组件生效-->
  125. <style lang="scss" scoped>
  126. .navbar {
  127. padding: 116rpx 30rpx 16rpx;
  128. box-sizing: border-box;
  129. position: relative;
  130. z-index: 9999;
  131. .title {
  132. font-size: 36rpx;
  133. color: #333;
  134. position: absolute;
  135. /* 绝对定位 */
  136. left: 50%;
  137. /* 从左侧50%开始 */
  138. transform: translateX(-50%);
  139. /* 向左移动自身宽度的一半 */
  140. }
  141. .icon-right {
  142. width: 48rpx;
  143. height: 48rpx;
  144. }
  145. .right-text {
  146. color: #333;
  147. font-size: 20rpx;
  148. }
  149. }
  150. </style>