Skip to content

Instantly share code, notes, and snippets.

@matheusmacedoap
Created November 23, 2024 23:57
Show Gist options
  • Save matheusmacedoap/1f6900de65beb11f9006b4a08bc27abf to your computer and use it in GitHub Desktop.
Save matheusmacedoap/1f6900de65beb11f9006b4a08bc27abf to your computer and use it in GitHub Desktop.
Quasar Automatic Routes
import { RouteRecordRaw } from 'vue-router';
const layoutsImport = import.meta.glob('layouts/**/*.vue');
const pagesImport = import.meta.glob('pages/**/*.vue');
const pageRegex = /\.\/pages\/(.*)\.vue$/;
const layoutRegex = /\.\/layouts\/(.*)\.vue$/;
const routes: RouteRecordRaw[] = [
{
path: '/:catchAll(.*)*',
component: () => import('pages/404/ErrorNotFound.vue'),
},
];
for (const pageimport in pagesImport) {
const page = pageRegex.exec(pageimport);
if (page) {
const pageName = page[1];
const layoutName = page[1].split('/')[0];
const path = `${pageName
.replace(layoutName, '')
.replace(/Index/g, '')
.replace(/Page$/, '')
.replace(/_/g, ':')
.toLowerCase()}`;
let layoutComponent;
for (const layoutImport in layoutsImport) {
const layout = layoutRegex.exec(layoutImport);
if (layout) {
if (layout[1].toLowerCase() === layoutName.concat('layout')) {
layoutComponent = layoutsImport[layoutImport];
break;
}
}
}
routes.push({
path: path,
component: layoutComponent,
children: [{ path: '', component: pagesImport[pageimport] }],
meta: { requiresAuth: false },
});
}
}
export default routes;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment