import Vue from ‘vue’
import Router from ‘vue-router’
Vue.use(Router)
/**
*构造动态路由
*/
let routes=[]
const contextInfo=require.context(’…/components/’,true,/.vue$/i)
contextInfo.keys().filter(fileName=>{
const pathConfig=contextInfo(fileName)
const split = fileName.split(’/’)||[’.vue’]
const splitStr = split[split.length-1]
const name = splitStr.substring(0,splitStr.length-4)||‘404’
console.log(‘routerName=’+name)
const path="/"+name
console.log(‘routerPath=’+path)
routes.push({
path,
name,
component:pathConfig.default
})
})
const router=new Router({
mode:‘history’,
routes
})
router.beforeEach((to, from, next) => {
console.log(’—>’+to.matched.length)
if (to.matched.length){
next()
}else {
next(’/404’)
}
})
export default router