u-nav-bar.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <view class="u-nav-bar" :style="{color: color,background: bg}">
  3. <uni-icons type="arrow-left" class="left-icon" size="30" :color="color" @click="toBack"></uni-icons>
  4. <view class="title" :title="title" v-if="title">
  5. {{title}}
  6. </view>
  7. </view>
  8. </template>
  9. <script>
  10. export default {
  11. name: "u-nav-bar",
  12. props: {
  13. title: { // 页面标题
  14. type: String,
  15. default: ''
  16. },
  17. color: { // 页面颜色
  18. type: String,
  19. default: 'white'
  20. },
  21. bg: { // 底色颜色
  22. type: String,
  23. default: ''
  24. },
  25. },
  26. computed: {
  27. showNavBar() {
  28. return Boolean(this.$listeners['on-left']) && this.prevPage
  29. }
  30. },
  31. data() {
  32. return {
  33. prevPage: null
  34. };
  35. },
  36. mounted() {
  37. let pages = getCurrentPages() // 获取栈实例
  38. let page = pages[pages.length - 1] // 获取当前页面的数据,包含页面路由
  39. this.prevPage = pages[pages.length - 2] // 获取上个页面的数据,包含页面路由
  40. },
  41. methods: {
  42. toBack() {
  43. if (Boolean(this.$listeners['on-left'])) {
  44. this.$emit('on-left')
  45. } else if (this.prevPage) {
  46. uni.navigateBack()
  47. } else {
  48. console.log('不存在上一页面')
  49. uni.switchTab({
  50. url: '/pages/index/index'
  51. });
  52. }
  53. }
  54. }
  55. }
  56. </script>
  57. <style lang="scss" scoped>
  58. .u-nav-bar {
  59. position: sticky;
  60. top: 0;
  61. left: 0;
  62. z-index: 5;
  63. width: 100%;
  64. height: $u-nav-bar-height;
  65. // padding: 0 32rpx;
  66. display: flex;
  67. flex-direction: column;
  68. justify-content: space-around;
  69. align-items: flex-start;
  70. .left-icon {
  71. display: inline-block;
  72. cursor: pointer;
  73. padding: 0 24rpx;
  74. }
  75. .title {
  76. font-size: 36rpx;
  77. font-weight: bold;
  78. padding: 0 32rpx;
  79. }
  80. }
  81. </style>