分离路由
Showing
2 changed files
with
47 additions
and
28 deletions
src/config/router.config.js
0 → 100644
1 | /** | ||
2 | * 基础路由 | ||
3 | * @type { *[] } | ||
4 | */ | ||
5 | export const constantRouterMap = [ | ||
6 | { | ||
7 | path: '/', | ||
8 | name: 'index', | ||
9 | component: () => import('@/views/home/index'), // 路由懒加载 | ||
10 | meta: { | ||
11 | title: '首页', // 页面标题 | ||
12 | keepAlive: false // keep-alive 标识 | ||
13 | } | ||
14 | }, | ||
15 | { | ||
16 | path: '/about', | ||
17 | name: 'about', | ||
18 | component: () => import('@/views/home/about'), | ||
19 | meta: { | ||
20 | title: '关于我', | ||
21 | keepAlive: false | ||
22 | } | ||
23 | } | ||
24 | ] |
1 | import Vue from 'vue' | 1 | import Vue from 'vue' |
2 | import Router from 'vue-router' | 2 | import Router from 'vue-router' |
3 | import { constantRouterMap } from '@/config/router.config' | ||
4 | |||
5 | // hack router push callback | ||
6 | const originalPush = Router.prototype.push | ||
7 | Router.prototype.push = function push (location, onResolve, onReject) { | ||
8 | if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject) | ||
9 | return originalPush.call(this, location).catch(err => err) | ||
10 | } | ||
3 | 11 | ||
4 | Vue.use(Router) | 12 | Vue.use(Router) |
5 | export const router = [ | ||
6 | { | ||
7 | path: '/', | ||
8 | name: 'index', | ||
9 | component: () => import('@/views/home/index'), // 路由懒加载 | ||
10 | meta: { | ||
11 | title: '首页', // 页面标题 | ||
12 | keepAlive: false // keep-alive 标识 | ||
13 | } | ||
14 | }, | ||
15 | { | ||
16 | path: '/about', | ||
17 | name: 'about', | ||
18 | component: () => import('@/views/home/about'), | ||
19 | meta: { | ||
20 | title: '关于我', | ||
21 | keepAlive: false | ||
22 | } | ||
23 | } | ||
24 | ] | ||
25 | 13 | ||
26 | const createRouter = () => | 14 | const createRouter = () => new Router({ |
27 | new Router({ | 15 | mode: 'history', |
28 | // mode: 'history', // 如果你是 history模式 需要配置vue.config.js publicPath | 16 | base: process.env.BASE_URL, |
29 | // base: '/app/', | 17 | scrollBehavior: () => ({ y: 0 }), |
30 | scrollBehavior: () => ({ y: 0 }), | 18 | routes: constantRouterMap |
31 | routes: router | 19 | }) |
32 | }) | 20 | |
21 | const router = createRouter() | ||
22 | |||
23 | // Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465 | ||
24 | export function resetRouter () { | ||
25 | const newRouter = createRouter() | ||
26 | router.matcher = newRouter.matcher // reset router | ||
27 | } | ||
33 | 28 | ||
34 | export default createRouter() | 29 | export default router | ... | ... |
-
Please register or sign in to post a comment