import { browser } from '@/helpers/utils'; import { showDialog } from 'vant'; import { createRouter, createWebHashHistory, Router } from 'vue-router'; import { postMessage } from '@/helpers/native-message'; import routes from './routes-common'; const router: Router = createRouter({ history: createWebHashHistory(), routes, scrollBehavior(to) { if (to.hash) { return { el: to.hash, behavior: 'smooth' }; } } }); router.beforeEach((to, from, next) => { document.title = (to.meta.title || '学生端') as any; // 团购报名页面区分两种类型的名称 if (to.path === '/student-register-apply') { document.title = to.query.rt === 'MUST_BUY_GOODS' ? '音乐(器乐)数字AI团购通道' : '报名通道'; } next(); }); let isOpen = false; router.onError(error => { if (error instanceof Error) { const isChunkLoadFailed = error.name.indexOf('chunk'); const targetPath = router.currentRoute.value.fullPath; console.log(error); if (isChunkLoadFailed && !isOpen) { isOpen = true; showDialog({ title: '更新提示', message: 'APP有更新请点击确定刷新页面?', confirmButtonColor: 'var(--van-primary)' }).then(() => { // on close if (browser().isApp) { postMessage({ api: 'back' }); } else { location.hash = targetPath; window.location.reload(); } }); } } }); export default router;