cl-page.uvue 1.7 KB

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