Last active
March 16, 2019 01:08
-
-
Save uriannrima/946355b0e47c1f1173e15fffa84c83a7 to your computer and use it in GitHub Desktop.
Automatic router registration per page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { RouteConfig } from 'vue-router' | |
import { Home } from './index'; | |
export const routes: RouteConfig[] = [ | |
{ | |
name: 'home', | |
path: '/home', | |
component: Home | |
}, | |
{ | |
path: '*', | |
component: Home | |
} | |
] | |
export default routes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { RouteConfig } from 'vue-router'; | |
/** Default Route. */ | |
const routes: RouteConfig[] = []; | |
/** Require Pages Context */ | |
const routeRequire = require.context( | |
'@/views/', | |
true, | |
/[Rr]oute.(js|ts)$/ | |
); | |
/** For each page */ | |
routeRequire.keys().forEach((fileName: string) => { | |
/** Get its config. */ | |
const routeConfig = routeRequire(fileName); | |
const configuredRoutes = routeConfig.default || routeConfig.routes || routeConfig; | |
if (configuredRoutes && Array.isArray(configuredRoutes)) { | |
configuredRoutes.forEach(route => { | |
/** Register each exported route. */ | |
routes.push(route); | |
}) | |
} | |
}) | |
export default routes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Later on, add something like "registration order".