vue路由分模块

router文件夹下index.js

import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/home'

Vue.use(Router)

const routerList = []
// 引入路由
function importAll(r) {
  r.keys().forEach(
    key => routerList.push(r(key).default)
  );
}

// require.context是webpack的API,动态引入文件
// 参数1.路径2.是否匹配子集3.规则(此规则需以.router.js结尾)
importAll(require.context('./', false, /\.router\.js/))

export const constantRoutes = [
  ...routerList
  {
    path: '/home',
    name: 'home',   
    component: home
  },
]

const router =  new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

export default router

子模块login.router.js

export default {
  path: '/login',
  name: 'login',
  component: () => import('@/login.vue'),
  children: [

  ]
}

 

posted @ 2022-04-15 17:23  申伸  阅读(277)  评论(0)    收藏  举报