Created
June 30, 2020 21:34
-
-
Save dorosch/c33fb1cf926309b2b1083ad85f3ae5a2 to your computer and use it in GitHub Desktop.
vue-router two views for one route
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 VueRouter from 'vue-router' | |
import Index from './components/Index' | |
import Dashboard from './components/Dashboard' | |
import authentificate from './auth' | |
const routes = [ | |
{ path: '/', name: 'index', component: Index, beforeEnter: check_if_auth }, | |
{ path: '/', name: 'dashboard', component: Dashboard, beforeEnter: check_if_auth } | |
] | |
const router = new VueRouter({ | |
mode: 'history', | |
routes | |
}) | |
export default router | |
function check_if_auth(to, from, next) { | |
/** | |
* Implements an interesting feature. When user isn't logged in, he sees | |
* the main page of the site and component `Index`. But when user is logged | |
* in, he sees the page of the `dashboard` site and component `Dashboard`. | |
*/ | |
if (to.name === 'index' && authentificate()) { | |
next({ name: 'dashboard' }) | |
} | |
else if (to.name === 'dashboard' && !authentificate()) { | |
next({ name: 'index' }) | |
} | |
else { | |
next() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment