uni-breadcrumb-item.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <view class="uni-breadcrumb-item">
  3. <view :class="{
  4. 'uni-breadcrumb-item--slot': true,
  5. 'uni-breadcrumb-item--slot-link': to && currentPage !== to
  6. }" @click="navTo">
  7. <slot />
  8. </view>
  9. <i v-if="separatorClass" class="uni-breadcrumb-item--separator" :class="separatorClass" />
  10. <text v-else class="uni-breadcrumb-item--separator">{{ separator }}</text>
  11. </view>
  12. </template>
  13. <script>
  14. /**
  15. * BreadcrumbItem 面包屑导航子组件
  16. * @property {String/Object} to 路由跳转页面路径/对象
  17. * @property {Boolean} replace 在使用 to 进行路由跳转时,启用 replace 将不会向 history 添加新记录(仅 h5 支持)
  18. */
  19. export default {
  20. data() {
  21. return {
  22. currentPage: ""
  23. }
  24. },
  25. options: {
  26. // #ifdef MP-TOUTIAO
  27. virtualHost: false,
  28. // #endif
  29. // #ifndef MP-TOUTIAO
  30. virtualHost: true
  31. // #endif
  32. },
  33. props: {
  34. to: {
  35. type: String,
  36. default: ''
  37. },
  38. replace:{
  39. type: Boolean,
  40. default: false
  41. }
  42. },
  43. inject: {
  44. uniBreadcrumb: {
  45. from: "uniBreadcrumb",
  46. default: null
  47. }
  48. },
  49. created(){
  50. const pages = getCurrentPages()
  51. const page = pages[pages.length-1]
  52. if(page){
  53. this.currentPage = `/${page.route}`
  54. }
  55. },
  56. computed: {
  57. separator() {
  58. return this.uniBreadcrumb.separator
  59. },
  60. separatorClass() {
  61. return this.uniBreadcrumb.separatorClass
  62. }
  63. },
  64. methods: {
  65. navTo() {
  66. const { to } = this
  67. if (!to || this.currentPage === to){
  68. return
  69. }
  70. if(this.replace){
  71. uni.redirectTo({
  72. url:to
  73. })
  74. }else{
  75. uni.navigateTo({
  76. url:to
  77. })
  78. }
  79. }
  80. }
  81. }
  82. </script>
  83. <style lang="scss">
  84. $uni-primary: #2979ff !default;
  85. $uni-base-color: #6a6a6a !default;
  86. $uni-main-color: #3a3a3a !default;
  87. .uni-breadcrumb-item {
  88. display: flex;
  89. align-items: center;
  90. white-space: nowrap;
  91. font-size: 14px;
  92. &--slot {
  93. color: $uni-base-color;
  94. padding: 0 10px;
  95. &-link {
  96. color: $uni-main-color;
  97. font-weight: bold;
  98. /* #ifndef APP-NVUE */
  99. cursor: pointer;
  100. /* #endif */
  101. &:hover {
  102. color: $uni-primary;
  103. }
  104. }
  105. }
  106. &--separator {
  107. font-size: 12px;
  108. color: $uni-base-color;
  109. }
  110. &:first-child &--slot {
  111. padding-left: 0;
  112. }
  113. &:last-child &--separator {
  114. display: none;
  115. }
  116. }
  117. </style>