cl-page.uvue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view
  4. :style="{ flex: 1 }"
  5. :scroll-top="scrollViewTop"
  6. :scroll-with-animation="true"
  7. @scroll="onScroll"
  8. >
  9. <cl-back-top v-if="backTop" @tap="scrollToTop"></cl-back-top>
  10. <theme></theme>
  11. <ui></ui>
  12. <slot></slot>
  13. </scroll-view>
  14. <!-- #endif -->
  15. <!-- #ifndef APP -->
  16. <cl-back-top v-if="backTop" @tap="scrollToTop"></cl-back-top>
  17. <theme></theme>
  18. <ui></ui>
  19. <slot></slot>
  20. <!-- #endif -->
  21. </template>
  22. <script setup lang="ts">
  23. import { computed, onMounted, ref, watch } from "vue";
  24. import { router, usePage } from "@/cool";
  25. import Theme from "./theme.uvue";
  26. import Ui from "./ui.uvue";
  27. import { locale, t } from "@/locale";
  28. import { config } from "@/config";
  29. defineOptions({
  30. name: "cl-page"
  31. });
  32. defineProps({
  33. // 是否显示回到顶部按钮
  34. backTop: {
  35. type: Boolean,
  36. default: config.backTop
  37. }
  38. });
  39. const page = usePage();
  40. // scroll-view 滚动位置
  41. const scrollViewTop = ref(0);
  42. // view 滚动事件
  43. function onScroll(e: UniScrollEvent) {
  44. page.triggerScroll(e.detail.scrollTop);
  45. }
  46. // 回到顶部
  47. function scrollToTop() {
  48. // #ifdef H5
  49. window.scrollTo({ top: 0, behavior: "smooth" });
  50. // #endif
  51. // #ifdef MP
  52. uni.pageScrollTo({
  53. scrollTop: 0,
  54. duration: 300
  55. });
  56. // #endif
  57. // #ifdef APP
  58. scrollViewTop.value = 0 + Math.random() / 1000;
  59. // #endif
  60. }
  61. onMounted(() => {
  62. // 标题多语言
  63. // #ifdef H5 || APP
  64. watch(
  65. computed(() => locale.value),
  66. () => {
  67. const style = router.route()?.style;
  68. if (style != null) {
  69. if (style.navigationBarTitleText != null) {
  70. uni.setNavigationBarTitle({
  71. title: t((style.navigationBarTitleText as string).replaceAll("%", ""))
  72. });
  73. }
  74. }
  75. },
  76. {
  77. immediate: true
  78. }
  79. );
  80. // #endif
  81. });
  82. </script>