This commit is contained in:
@@ -3,18 +3,30 @@ import VueRouter from 'vue-router'
|
||||
import Dashboard from '@/views/Dashboard'
|
||||
import Welcome from "@/views/Welcome";
|
||||
|
||||
Vue.use(VueRouter)
|
||||
import store from '@/store'
|
||||
|
||||
Vue.use(VueRouter);
|
||||
|
||||
const routes = [
|
||||
{
|
||||
path: '/',
|
||||
path: '/welcome',
|
||||
name: 'Welcome',
|
||||
component: Welcome
|
||||
component: Welcome,
|
||||
meta: {
|
||||
allowVisit(loggedin) {
|
||||
return !loggedin;
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
path: '/dashboard',
|
||||
path: '/',
|
||||
name: 'Dashboard',
|
||||
component: Dashboard
|
||||
component: Dashboard,
|
||||
meta: {
|
||||
allowVisit(loggedin) {
|
||||
return loggedin;
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
path: '/about',
|
||||
@@ -22,7 +34,12 @@ const routes = [
|
||||
// route level code-splitting
|
||||
// this generates a separate chunk (about.[hash].js) for this route
|
||||
// which is lazy-loaded when the route is visited.
|
||||
component: () => import(/* webpackChunkName: "about" */ '@/views/About.vue')
|
||||
component: () => import(/* webpackChunkName: "about" */ '@/views/About.vue'),
|
||||
meta: {
|
||||
allowVisit() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
@@ -32,4 +49,25 @@ const router = new VueRouter({
|
||||
routes
|
||||
})
|
||||
|
||||
|
||||
router.beforeEach((to, from, next) => {
|
||||
|
||||
const loggedin = store.getters.isLoggedIn;
|
||||
|
||||
const visitAllowed = to.matched.some(record => record.meta.allowVisit(loggedin))
|
||||
|
||||
if (visitAllowed) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
if (loggedin) {
|
||||
next({name: 'Dashboard'})
|
||||
} else {
|
||||
next({name: 'Welcome'})
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
|
||||
export default router
|
||||
|
||||
Reference in New Issue
Block a user