u-empty.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <view
  3. class="u-empty"
  4. :style="[emptyStyle]"
  5. v-if="show"
  6. >
  7. <u-icon
  8. v-if="!isSrc"
  9. :name="mode === 'message' ? 'chat' : `empty-${mode}`"
  10. :size="iconSize"
  11. :color="iconColor"
  12. margin-top="14"
  13. ></u-icon>
  14. <image
  15. v-else
  16. :style="{
  17. width: $u.addUnit(width),
  18. height: $u.addUnit(height),
  19. }"
  20. :src="icon"
  21. mode="widthFix"
  22. ></image>
  23. <text
  24. class="u-empty__text"
  25. :style="[textStyle]"
  26. >{{ text ? text : icons[mode] }}
  27. </text>
  28. <view class="u-empty__wrap" v-if="$slots.default || $slots.$default">
  29. <slot/>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. import props from './props.js';
  35. /**
  36. * empty 内容为空
  37. * @description 该组件用于需要加载内容,但是加载的第一页数据就为空,提示一个"没有内容"的场景, 我们精心挑选了十几个场景的图标,方便您使用。
  38. * @tutorial https://www.uviewui.com/components/empty.html
  39. * @property {String} icon 内置图标名称,或图片路径,建议绝对路径
  40. * @property {String} text 提示文字
  41. * @property {String} textColor 文字颜色 (默认 '#c0c4cc' )
  42. * @property {String | Number} textSize 文字大小 (默认 14 )
  43. * @property {String} iconColor 图标的颜色 (默认 '#c0c4cc' )
  44. * @property {String | Number} iconSize 图标的大小 (默认 90 )
  45. * @property {String} mode 选择预置的图标类型 (默认 'data' )
  46. * @property {String | Number} width 图标宽度,单位px (默认 160 )
  47. * @property {String | Number} height 图标高度,单位px (默认 160 )
  48. * @property {Boolean} show 是否显示组件 (默认 true )
  49. * @property {String | Number} marginTop 组件距离上一个元素之间的距离,默认px单位 (默认 0 )
  50. * @property {Object} customStyle 定义需要用到的外部样式
  51. *
  52. * @event {Function} click 点击组件时触发
  53. * @event {Function} close 点击关闭按钮时触发
  54. * @example <u-empty text="所谓伊人,在水一方" mode="list"></u-empty>
  55. */
  56. export default {
  57. name: "u-empty",
  58. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  59. data() {
  60. return {
  61. icons: {
  62. car: '购物车为空',
  63. page: '页面不存在',
  64. search: '没有搜索结果',
  65. address: '没有收货地址',
  66. wifi: '没有WiFi',
  67. order: '订单为空',
  68. coupon: '没有优惠券',
  69. favor: '暂无收藏',
  70. permission: '无权限',
  71. history: '无历史记录',
  72. news: '无新闻列表',
  73. message: '消息列表为空',
  74. list: '列表为空',
  75. data: '数据为空',
  76. comment: '暂无评论',
  77. }
  78. }
  79. },
  80. computed: {
  81. // 组件样式
  82. emptyStyle() {
  83. const style = {}
  84. style.marginTop = uni.$u.addUnit(this.marginTop)
  85. // 合并customStyle样式,此参数通过mixin中的props传递
  86. return uni.$u.deepMerge(uni.$u.addStyle(this.customStyle), style)
  87. },
  88. // 文本样式
  89. textStyle() {
  90. const style = {}
  91. style.color = this.textColor
  92. style.fontSize = uni.$u.addUnit(this.textSize)
  93. return style
  94. },
  95. // 判断icon是否图片路径
  96. isSrc() {
  97. return this.icon.indexOf('/') >= 0
  98. }
  99. }
  100. }
  101. </script>
  102. <style lang="scss" scoped>
  103. @import '../../libs/css/components.scss';
  104. $u-empty-text-margin-top: 20 rpx !default;
  105. $u-empty-slot-margin-top: 20 rpx !default;
  106. .u-empty {
  107. @include flex;
  108. flex-direction: column;
  109. justify-content: center;
  110. align-items: center;
  111. &__text {
  112. @include flex;
  113. justify-content: center;
  114. align-items: center;
  115. margin-top: $u-empty-text-margin-top;
  116. }
  117. }
  118. .u-slot-wrap {
  119. @include flex;
  120. justify-content: center;
  121. align-items: center;
  122. margin-top: $u-empty-slot-margin-top;
  123. }
  124. </style>