tn-empty.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <template>
  2. <view v-if="show" class="tn-empty-class tn-empty" :style="[emptyStyle]">
  3. <view
  4. v-if="!isImage"
  5. class="tn-empty__icon"
  6. :class="[icon ? `tn-icon-${icon}` : `tn-icon-empty-${mode}`]"
  7. :style="[iconStyle]"
  8. ></view>
  9. <image
  10. v-else
  11. class="tn-empty__image"
  12. :style="[imageStyle]"
  13. :src="icon"
  14. mode="widthFix"
  15. ></image>
  16. <view
  17. class="tn-empty__text"
  18. :style="[textStyle]"
  19. >{{ text ? text : icons[mode]}}</view>
  20. <view v-if="$slots.default || $slots.$default" class="tn-empty__slot">
  21. <slot/>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. name: 'tn-empty',
  28. props: {
  29. // 显示空白页
  30. show: {
  31. type: Boolean,
  32. default: true
  33. },
  34. // 内置icon的名称
  35. // 图片路径,建议使用绝对路径
  36. icon: {
  37. type: String,
  38. default: ''
  39. },
  40. // 预置图标类型
  41. mode: {
  42. type: String,
  43. default: 'data'
  44. },
  45. // 提示文字
  46. text: {
  47. type: String,
  48. default: ''
  49. },
  50. // 文字颜色
  51. textColor: {
  52. type: String,
  53. default: ''
  54. },
  55. // 文字大小,单位rpx
  56. textSize: {
  57. type: Number,
  58. default: 0
  59. },
  60. // 图标颜色
  61. iconColor: {
  62. type: String,
  63. default: ''
  64. },
  65. // 图标大小,单位rpx
  66. iconSize: {
  67. type: Number,
  68. default: 0
  69. },
  70. // 图片宽度(当图标为图片时生效),单位rpx
  71. imgWidth: {
  72. type: Number,
  73. default: 0
  74. },
  75. // 图片高度(当图标为图片时生效),单位rpx
  76. imgHeight: {
  77. type: Number,
  78. default: 0
  79. },
  80. // 自定义组件样式
  81. customStyle: {
  82. type: Object,
  83. default() {
  84. return {}
  85. }
  86. }
  87. },
  88. computed: {
  89. emptyStyle() {
  90. let style = {}
  91. Object.assign(style, this.customStyle)
  92. return style
  93. },
  94. iconStyle() {
  95. let style = {}
  96. if (this.iconSize) {
  97. style.fontSize = this.iconSize + 'rpx'
  98. }
  99. if (this.iconColor) {
  100. style.color = this.iconColor
  101. }
  102. return style
  103. },
  104. imageStyle() {
  105. let style = {}
  106. if (this.imgWidth) {
  107. style.width = this.imgWidth + 'rpx'
  108. }
  109. if (this.imgHeight) {
  110. style.height = this.imgHeight + 'rpx'
  111. }
  112. return style
  113. },
  114. textStyle() {
  115. let style = {}
  116. if (this.textColor) {
  117. style.color = this.textColor
  118. }
  119. if (this.textSize) {
  120. style.fontSize = this.textSize + 'rpx'
  121. }
  122. return style
  123. },
  124. // 判断传递的icon是否为图片
  125. isImage() {
  126. return this.icon.indexOf('/') >= 0
  127. }
  128. },
  129. data() {
  130. return {
  131. icons: {
  132. cart: '购物车为空',
  133. page: '页面不存在',
  134. search: '搜索结果为空',
  135. address: '地址为空',
  136. network: '网络不通',
  137. order: '订单为空',
  138. coupon: '优惠券为空',
  139. favor: '暂无收藏',
  140. permission: '无权限',
  141. history: '历史记录为空',
  142. message: '暂无消息',
  143. list: '列表为空',
  144. data: '暂无数据',
  145. comment: '暂无评论'
  146. }
  147. }
  148. }
  149. }
  150. </script>
  151. <style lang="scss" scoped>
  152. .tn-empty {
  153. display: flex;
  154. flex-direction: column;
  155. align-items: center;
  156. justify-content: center;
  157. &__icon {
  158. margin-top: 14rpx;
  159. color: #AAAAAA;
  160. font-size: 90rpx;
  161. }
  162. &__image {
  163. width: 160rpx;
  164. height: 160rpx;
  165. }
  166. &__text {
  167. display: flex;
  168. flex-direction: column;
  169. align-items: center;
  170. justify-content: center;
  171. margin-top: 20rpx;
  172. color: #AAAAAA;
  173. font-size: 30rpx;
  174. }
  175. &__slot {
  176. display: flex;
  177. flex-direction: column;
  178. align-items: center;
  179. justify-content: center;
  180. margin-top: 20rpx;
  181. }
  182. }
  183. </style>