Empty.vue 1.2 KB

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