Skip to content

Instantly share code, notes, and snippets.

@uriannrima
Last active March 16, 2019 01:08
Show Gist options
  • Save uriannrima/946355b0e47c1f1173e15fffa84c83a7 to your computer and use it in GitHub Desktop.
Save uriannrima/946355b0e47c1f1173e15fffa84c83a7 to your computer and use it in GitHub Desktop.
Automatic router registration per page
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
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
@uriannrima
Copy link
Author

uriannrima commented Mar 16, 2019

Later on, add something like "registration order".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment