top-return.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <!-- 头部返回按钮组件 -->
  2. <template>
  3. <view class="back" :style="{height: capsuleHeight+ 'px',top:capsuleTop+'px'}">
  4. <view v-if="!refresh">
  5. <u-icon name="arrow-left" v-if="show === 'back'" :color="color" size="24" @click="backIconHandler"></u-icon>
  6. <u-icon name="home-fill" v-if="show === 'home'" :color="color" size="24" @click="backHomeHandler"></u-icon>
  7. </view>
  8. <view v-if="refresh">
  9. <u-icon name="arrow-left" v-if="show === 'back'" :color="color" size="24" @click="backIconHandlerRefresh"></u-icon>
  10. <u-icon name="home-fill" v-if="show === 'home'" :color="color" size="24" @click="backIconHandlerRefresh"></u-icon>
  11. </view>
  12. <view class="back-text" :style="{color: color}">{{ text }}</view>
  13. </view>
  14. </template>
  15. <script>
  16. export default {
  17. name: "top-return",
  18. props: {
  19. text: {
  20. type: String,
  21. default: ''
  22. },
  23. color: {
  24. type: String,
  25. default: '#303133'
  26. },
  27. show: {
  28. type: String,
  29. default: 'back'
  30. },
  31. refresh: {
  32. type: Boolean,
  33. default: false
  34. }
  35. },
  36. data() {
  37. return {
  38. //胶囊
  39. capsuleHeight: 0,
  40. capsuleTop: 0,
  41. };
  42. },
  43. created() {
  44. this.getCapsuleSite()
  45. },
  46. methods: {
  47. backIconHandler() {
  48. uni.navigateBack()
  49. },
  50. backIconHandlerRefresh() {
  51. let pages = getCurrentPages(); // 当前页面
  52. let beforePage = pages[pages.length - 2]; // 上一页
  53. uni.navigateBack({
  54. success: function() {
  55. beforePage.onLoad()// 执行上一页的onLoad方法
  56. }
  57. })
  58. },
  59. backHomeHandler() {
  60. uni.switchTab({
  61. url: '/pages/tab_home/tab_home'
  62. })
  63. },
  64. //获取胶囊位置信息
  65. getCapsuleSite() {
  66. let res = uni.getMenuButtonBoundingClientRect()
  67. this.capsuleHeight = res.height
  68. this.capsuleTop = res.top
  69. }
  70. }
  71. }
  72. </script>
  73. <style lang="scss">
  74. .back {
  75. position: relative;
  76. padding: 0 0 0 20rpx;
  77. display: flex;
  78. align-items: center;
  79. z-index: 999;
  80. .back-text {
  81. margin:0 0 5rpx 20rpx;
  82. font-size: 40rpx;
  83. font-weight: bold;
  84. z-index: 999;
  85. }
  86. }
  87. </style>