main.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { createSSRApp } from 'vue';
  2. import { createPinia } from 'pinia';
  3. import App from './App.vue';
  4. import { useUserStore } from './store/user';
  5. // #ifdef H5
  6. import VConsole from 'vconsole';
  7. // #endif
  8. /**
  9. * 在 #app 之外创建启动态浮层 DOM(独立于 Vue 渲染树),
  10. * App.vue 通过 watch 控制其显示/隐藏/内容。
  11. *
  12. * 原因:uni-app H5 的 page HOC 会吞掉 App.vue 模板里非 router-view 子节点,
  13. * 把 splash 放在 Vue 渲染树里会导致 v-if 完全不生效。
  14. */
  15. function ensureBootSplash() {
  16. if (typeof document === 'undefined') return;
  17. if (document.getElementById('boot-splash')) return;
  18. const div = document.createElement('div');
  19. div.id = 'boot-splash';
  20. div.className = 'boot-splash';
  21. div.style.display = 'none';
  22. document.body.appendChild(div);
  23. }
  24. // H5 平台下立即创建(其它平台不需要此浮层)
  25. // #ifdef H5
  26. ensureBootSplash();
  27. // #endif
  28. export function createApp() {
  29. // 再次确保(main.ts 在不同入口可能被多次调用)
  30. // #ifdef H5
  31. ensureBootSplash();
  32. // #endif
  33. const app = createSSRApp(App);
  34. const pinia = createPinia();
  35. app.use(pinia);
  36. // 初始化用户状态
  37. const userStore = useUserStore();
  38. userStore.initUser();
  39. // #ifdef H5
  40. // vConsole 开启,用于调试微信支付问题
  41. new VConsole();
  42. // #endif
  43. return {
  44. app,
  45. pinia,
  46. };
  47. }