Empty.vue 1.3 KB

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