/** * 判断当前应用路由是否匹配到 * @param router * @returns {boolean} */ function getHasRouteMatched (router) { var pathName = location.pathname var baseName = router.options.base var pathIndex = pathName.indexOf(baseName) // 存在别名情况,需要额外匹配 var nowPath = pathName if (pathIndex > -1) { nowPath = pathName.slice(pathIndex + baseName.length) } var { route } = router.resolve({ path: nowPath }) var hasRouteMatched = route.matched.length > 0 return hasRouteMatched } function sendNotFindPageError (content, options) { if (window.Sentry && typeof window.Sentry.captureMessage === 'function') { Sentry.captureMessage(content, options) } } /** * 子应用 404 页面拦截 * @param router */ function addNotFindPage(router) { var notFindPage = "/page_error/404.html"; router.beforeEach((to, from, next) => { // vue-router相关路由,history.state中必有key var vueRouterHistory = history.state && history.state.key if (to.matched.length === 0 && vueRouterHistory) { sendNotFindPageError(new Error('Error getHasRouteMatched beforeEach'), { level: 'error', tags: { href: location.href }, extra: { router: router, to: to, from: from } }) if (window.jQuery) { $.ajax({ url: '/data-export/help-report', method: 'post', data: { type: 'Error getHasRouteMatched beforeEach', extra: JSON.stringify({ router: router, to: to, from: from }) } }) } window.top.location.replace(notFindPage); } else { next(); } }); if (!getHasRouteMatched(router)) { sendNotFindPageError(new Error('Error getHasRouteMatched'), { level: 'error', tags: { href: location.href }, extra: { router: router } }) if (window.jQuery) { $.ajax({ url: '/data-export/help-report', method: 'post', data: { type: 'Error getHasRouteMatched', extra: JSON.stringify({ router: router, }) } }) } window.top.location.replace(notFindPage); } }