index.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. router.beforeEach((to, from, next) => {
  19. document.title = (to.meta.title || '学生端') as any;
  20. next();
  21. });
  22. let isOpen = false;
  23. router.onError(error => {
  24. if (error instanceof Error) {
  25. const isChunkLoadFailed = error.name.indexOf('chunk');
  26. const targetPath = router.currentRoute.value.fullPath;
  27. console.log(error);
  28. if (isChunkLoadFailed && !isOpen) {
  29. isOpen = true;
  30. showDialog({
  31. title: '更新提示',
  32. message: 'APP有更新请点击确定刷新页面?',
  33. confirmButtonColor: 'var(--van-primary)'
  34. }).then(() => {
  35. // on close
  36. if (browser().isApp) {
  37. postMessage({ api: 'back' });
  38. } else {
  39. location.hash = targetPath;
  40. window.location.reload();
  41. }
  42. });
  43. }
  44. }
  45. });
  46. export default router;