Last active
April 23, 2020 11:01
-
-
Save dlodeprojuicer/66e3f9c1ed8665daea8f9a60915779e9 to your computer and use it in GitHub Desktop.
Full dynamic router loop
This file contains hidden or 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
const requireModule = require.context("./views", false, /\.vue$/); | |
let routes = []; | |
requireModule.keys().map(file => { | |
const moduleName = file.split("/")[1].split(".vue")[0]; | |
const url = moduleName.replace(/([a-zA-Z])(?=[A-Z])/g, "$1-").toLowerCase(); | |
if (moduleName.match(new RegExp(/^[A-Z]/)) === null) { | |
throw `Page file name must start with uppercase - ${file}`; | |
} | |
const viewComponent = requireModule(file).default; | |
if (viewComponent.name !== url) { | |
throw `[${file}] Page file name is not consistant with module name - expected ${url}, found ${viewComponent.name}`; | |
} | |
routes.push({ | |
path: `/${url}`, | |
name: url, | |
component: () => import(`./views/${moduleName}.vue`), | |
meta: { | |
requiresAuth: setAuth(moduleName), | |
title: viewComponent.title, | |
} | |
}); | |
}); | |
// for Vue Router | |
export default new VueRouter({ | |
routes | |
}); | |
// for Ionic Vue Router | |
export default new IonicVueRouter({ | |
routes | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment