index-teacher.ts 1.2 KB

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