cwg-match-media.vue 919 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <template>
  2. <template v-if="shouldShow">
  3. <slot />
  4. </template>
  5. </template>
  6. <script setup lang="ts">
  7. import { ref, computed, onMounted, onUnmounted } from 'vue';
  8. const props = defineProps({
  9. minWidth: {
  10. type: Number,
  11. default: undefined
  12. },
  13. maxWidth: {
  14. type: Number,
  15. default: undefined
  16. }
  17. })
  18. const shouldShow = ref(true)
  19. function checkWidth() {
  20. const width = uni.getSystemInfoSync().windowWidth
  21. shouldShow.value = (props.minWidth === undefined || width >= props.minWidth) && (props.maxWidth === undefined || width <= props.maxWidth)
  22. }
  23. checkWidth()
  24. let resizeListener: any
  25. onMounted(() => {
  26. resizeListener = () => checkWidth()
  27. uni.onWindowResize && uni.onWindowResize(resizeListener)
  28. })
  29. onUnmounted(() => {
  30. uni.offWindowResize && uni.offWindowResize(resizeListener)
  31. })
  32. </script>
  33. <style lang="scss" scoped>
  34. @import "@/uni.scss";
  35. </style>