main.js中
// 插件-路由 import VueRouter from 'vue-router' Vue.use(VueRouter) import {routes} from "./routes"; //自动注册路由- page目录下的一级vue文件 require.context('./page', true, /\.vue$/).keys().forEach(fileName => { let componentName = fileName.replace(/^\.\//,'').replace(/\.vue$/,''); let temp = { path: '/'+componentName, component:resolve => require(["@/page/"+componentName], resolve) } routes.push(temp) }); const router = new VueRouter({ routes })
page文件夹和main.js同级