uni-breadcrumb-item.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. virtualHost: true
  27. },
  28. props: {
  29. to: {
  30. type: String,
  31. default: ''
  32. },
  33. replace:{
  34. type: Boolean,
  35. default: false
  36. }
  37. },
  38. inject: {
  39. uniBreadcrumb: {
  40. from: "uniBreadcrumb",
  41. default: null
  42. }
  43. },
  44. created(){
  45. const pages = getCurrentPages()
  46. const page = pages[pages.length-1]
  47. if(page){
  48. this.currentPage = `/${page.route}`
  49. }
  50. },
  51. computed: {
  52. separator() {
  53. return this.uniBreadcrumb.separator
  54. },
  55. separatorClass() {
  56. return this.uniBreadcrumb.separatorClass
  57. }
  58. },
  59. methods: {
  60. navTo() {
  61. const { to } = this
  62. if (!to || this.currentPage === to){
  63. return
  64. }
  65. if(this.replace){
  66. uni.redirectTo({
  67. url:to
  68. })
  69. }else{
  70. uni.navigateTo({
  71. url:to
  72. })
  73. }
  74. }
  75. }
  76. }
  77. </script>
  78. <style lang="scss">
  79. $uni-primary: #2979ff !default;
  80. $uni-base-color: #6a6a6a !default;
  81. $uni-main-color: #3a3a3a !default;
  82. .uni-breadcrumb-item {
  83. display: flex;
  84. align-items: center;
  85. white-space: nowrap;
  86. font-size: 14px;
  87. &--slot {
  88. color: $uni-base-color;
  89. padding: 0 10px;
  90. &-link {
  91. color: $uni-main-color;
  92. font-weight: bold;
  93. /* #ifndef APP-NVUE */
  94. cursor: pointer;
  95. /* #endif */
  96. &:hover {
  97. color: $uni-primary;
  98. }
  99. }
  100. }
  101. &--separator {
  102. font-size: 12px;
  103. color: $uni-base-color;
  104. }
  105. &:first-child &--slot {
  106. padding-left: 0;
  107. }
  108. &:last-child &--separator {
  109. display: none;
  110. }
  111. }
  112. </style>