From 44277e69758f09c245340fa7761f342c12dc59c3 Mon Sep 17 00:00:00 2001 From: xiaodi Date: Sun, 31 May 2020 21:03:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E7=A6=BB=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/router.config.js | 24 +++++++++++++++++ src/router/index.js | 51 +++++++++++++++++-------------------- 2 files changed, 47 insertions(+), 28 deletions(-) create mode 100644 src/config/router.config.js diff --git a/src/config/router.config.js b/src/config/router.config.js new file mode 100644 index 0000000..add8534 --- /dev/null +++ b/src/config/router.config.js @@ -0,0 +1,24 @@ +/** + * 基础路由 + * @type { *[] } + */ +export const constantRouterMap = [ + { + path: '/', + name: 'index', + component: () => import('@/views/home/index'), // 路由懒加载 + meta: { + title: '首页', // 页面标题 + keepAlive: false // keep-alive 标识 + } + }, + { + path: '/about', + name: 'about', + component: () => import('@/views/home/about'), + meta: { + title: '关于我', + keepAlive: false + } + } +] diff --git a/src/router/index.js b/src/router/index.js index aa5a9f7..7e9538c 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -1,34 +1,29 @@ import Vue from 'vue' import Router from 'vue-router' +import { constantRouterMap } from '@/config/router.config' + +// hack router push callback +const originalPush = Router.prototype.push +Router.prototype.push = function push (location, onResolve, onReject) { + if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject) + return originalPush.call(this, location).catch(err => err) +} Vue.use(Router) -export const router = [ - { - path: '/', - name: 'index', - component: () => import('@/views/home/index'), // 路由懒加载 - meta: { - title: '首页', // 页面标题 - keepAlive: false // keep-alive 标识 - } - }, - { - path: '/about', - name: 'about', - component: () => import('@/views/home/about'), - meta: { - title: '关于我', - keepAlive: false - } - } -] -const createRouter = () => - new Router({ - // mode: 'history', // 如果你是 history模式 需要配置vue.config.js publicPath - // base: '/app/', - scrollBehavior: () => ({ y: 0 }), - routes: router - }) +const createRouter = () => new Router({ + mode: 'history', + base: process.env.BASE_URL, + scrollBehavior: () => ({ y: 0 }), + routes: constantRouterMap +}) -export default createRouter() +const router = createRouter() + +// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465 +export function resetRouter () { + const newRouter = createRouter() + router.matcher = newRouter.matcher // reset router +} + +export default router