index.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { browser } from '@/helpers/utils';
  2. import { showDialog } from 'vant';
  3. import { createRouter, createWebHashHistory, Router } from 'vue-router';
  4. import { postMessage } from '@/helpers/native-message';
  5. import routes from './routes-common';
  6. const router: Router = createRouter({
  7. history: createWebHashHistory(),
  8. routes,
  9. scrollBehavior(to) {
  10. if (to.hash) {
  11. return {
  12. el: to.hash,
  13. behavior: 'smooth'
  14. };
  15. }
  16. }
  17. });
  18. let isOpen = false;
  19. router.onError(error => {
  20. if (error instanceof Error) {
  21. const isChunkLoadFailed = error.name.indexOf('chunk');
  22. const targetPath = router.currentRoute.value.fullPath;
  23. console.log(error);
  24. if (isChunkLoadFailed && !isOpen) {
  25. isOpen = true;
  26. showDialog({
  27. title: '更新提示',
  28. message: 'APP有更新请点击确定刷新页面?',
  29. confirmButtonColor: 'var(--van-primary)'
  30. }).then(() => {
  31. // on close
  32. if (browser().isApp) {
  33. postMessage({ api: 'back' });
  34. } else {
  35. location.hash = targetPath;
  36. window.location.reload();
  37. }
  38. });
  39. }
  40. }
  41. });
  42. export default router;