Created
April 6, 2023 12:41
-
-
Save OctaneInteractive/0fb113b3904333353a869ffd825365d7 to your computer and use it in GitHub Desktop.
Router in Vue 2
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 Vue from 'vue' | |
import Router from 'vue-router' | |
import Store from '../store' | |
Vue.use(Router) | |
let router = new Router({ | |
mode: 'history', | |
routes: [ | |
/** | |
* Home. | |
*/ | |
{ | |
path: '/', | |
name: 'Home', | |
component: () => import(/* webpackChunkName: "Home" */ '@/components/Home'), | |
meta: { | |
isGuest: true, | |
title: 'Under Cloud is the ultimate digital research assistant' | |
} | |
}, | |
{ | |
path: '/dashboard', | |
name: 'Dashboard', | |
component: () => import(/* webpackChunkName: "Dashboard" */ '@/components/dashboard/Dashboard'), | |
meta: { | |
requiresAuth: true | |
} | |
}, | |
{ | |
path: '/diagnostics/show', | |
name: 'ShowDiagnostics', | |
component: () => import(/* webpackChunkName: "ShowDiagnostics" */ '@/components/dashboard/ShowDiagnostics'), | |
beforeEnter: (to, from, next) => { | |
if (Store.getters.getUserRole.userRole > 1) { | |
next({ | |
name: 'Dashboard' | |
}) | |
} else { | |
next() | |
} | |
}, | |
meta: { | |
requiresAuth: true | |
} | |
}, | |
/** | |
* Assets. | |
*/ | |
{ | |
path: '/assets', | |
name: 'ShowFolders', | |
component: () => import(/* webpackChunkName: "ShowFolders" */ '@/components/assets/ShowFolders'), | |
props: route => { | |
document.title = (() => { | |
return Store.getters.getPageTitle || 'Assets' | |
})() | |
return { | |
folderId: route.params.folderId | |
} | |
}, | |
children: [ | |
{ | |
path: '/assets/folders/:folderId', | |
name: 'ShowAssets', | |
component: () => import(/* webpackChunkName: "ShowAssets" */ '@/components/assets/ShowAssets'), | |
props: route => { | |
return { | |
folderId: route.params.folderId | |
} | |
}, | |
meta: { | |
requiresAuth: true | |
} | |
} | |
], | |
meta: { | |
requiresAuth: true | |
} | |
} | |
] | |
}) | |
router.beforeEach((to, from, next) => { | |
if (to.matched.some(record => record.meta.requiresAuth)) { | |
if (!Store.getters.getSignedInStatus.isUserSignedIn) { | |
next({ | |
name: 'Home' | |
}) | |
} else { | |
next() | |
} | |
next() | |
} else if (to.matched.some(record => record.meta.isGuest)) { | |
next() | |
} | |
}) | |
router.afterEach((to, from) => { | |
// Use `nextTick()` to handle router history ( https://github.com/vuejs/vue-router/issues/914#issuecomment-384477609 ). | |
Vue.nextTick(() => { | |
document.title = to.meta.title || (() => { | |
return Store.getters.getPageTitle || 'Under Cloud' | |
})() | |
}) | |
}) | |
export default router |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An example of the backend of to the frontend framework, Vue. Here, the router is used to route authorized users to the appropriate components while routing everyone to somewhere else. I'm using the
beforeEach()
route guard to handle any exceptions.Also, it's possible to name components to assist Webpack when it comes to code splitting: