在src目录下,新建router.js
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
Vue.use(Router)
const files = require.context('./router',false,/\.js$/)
let routes = [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld
}
]
files.keys().forEach(key =>{
const tmp = files(key).default
console.log(files(key).default)
routes = routes.concat(...tmp)
})
console.log(routes)
export default new Router({
mode: 'history',
routes
})
修改main.js把
import router from './router'
改成
import router from './router.js'