import Vue from 'vue' import VueRouter from 'vue-router' import Home from '@/views/Home.vue' import Login from "@/views/Login"; import store from '@/store' Vue.use(VueRouter) const routes = [ { path: '/', name: 'Home', component: Home, meta: { allowVisit(authorized) { return authorized; } } }, { path: '/about', name: 'About', // 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'), meta: { allowVisit() { return true; } } }, { path: '/login', name: 'Login', component: Login, meta: { allowVisit(authorized) { return !authorized; } } } ] const router = new VueRouter({ mode: 'history', base: process.env.BASE_URL, routes }) router.beforeEach((to, from, next) => { const authorized = store.getters.isLoggedIn; const visitAllowed = to.matched.some(record => record.meta.allowVisit(authorized)) if (visitAllowed) { next(); return; } if (authorized) { next({name: 'Home'}) } else { next({name: 'Login'}) } }) export default router