Empty.vue 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <template>
  2. <view class="empty-container" :style="{ height: containerHeight }">
  3. <text class="empty-text">{{ text }}</text>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. name: "Empty",
  9. props: {
  10. text: {
  11. type: String,
  12. default: "暂无数据", // 默认文字
  13. },
  14. },
  15. data() {
  16. return {
  17. topHeight: 0, // 到顶部的距离
  18. };
  19. },
  20. computed: {
  21. // 计算动态的容器高度
  22. containerHeight() {
  23. return `calc(100vh - ${this.topHeight}px)`;
  24. },
  25. },
  26. mounted() {
  27. this.$nextTick(() => {
  28. this.getElementTopDistance();
  29. });
  30. },
  31. methods: {
  32. getElementTopDistance() {
  33. let _this = this;
  34. const query = uni.createSelectorQuery().in(_this);
  35. query
  36. .select(".empty-container")
  37. .boundingClientRect((data) => {
  38. if (data) {
  39. this.topHeight = data.top; // 记录到顶部的距离
  40. }
  41. })
  42. .exec();
  43. },
  44. },
  45. };
  46. </script>
  47. <style scoped lang="less">
  48. .empty-container {
  49. display: flex;
  50. flex-direction: column;
  51. justify-content: center;
  52. align-items: center;
  53. width: 100vw; /* 占满屏幕宽度 */
  54. background-color: #f5f5f5;
  55. overflow: hidden;
  56. margin: 0;
  57. padding: 0;
  58. box-sizing: border-box;
  59. text-align: center;
  60. }
  61. .empty-text {
  62. color: #666;
  63. font-size: 16px;
  64. }
  65. </style>