not sure if I am doing something wrong. I have setup vue-router successfully but I have only one problem, let me explain:
I use meta field to specify whether or not a route requires authentication, if user is not authenticated the router load login page "/auth/login" when the user is authenticated the router load main page "/". All routes can be accessed through a drawer and only three can be accessed trough a menu in an appBar ("pages/user", "/admon/usuarios", "pages/login")
The first time to I access "/admon/usuarios" works fine. from "/" go to "/admon/usuarios"
If I change the route every thing works fine: from "/admon/usuarios" to any route
When I returned to "/" and I want to return "/admon/usuarios" the router go to "/admon/usuarios" and immediately go to "/" as we can see in next image of history router at 18:14:02 & 18:14:03
This is the router code:
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const routes = [
{
path: '/pages',
component: () => import('@/views/pages/Index'),
children: [
{
name: 'Login',
path: 'login',
component: () => import('@/views/pages/Login'),
},
{
name: 'Lock',
path: 'lock',
component: () => import('@/views/pages/Lock'),
},
{
name: 'Register',
path: 'register',
component: () => import('@/views/pages/Register'),
},
],
},
{
path: '/',
component: () => import('@/views/dashboard/Index'),
meta: {
requiresAuth: true,
},
children: [
{
name: 'Dashboard',
path: '',
meta: {
requiresAuth: true,
},
component: () => import('@/views/dashboard/Dashboard'),
},
{
name: 'Traslados',
path: 'ventas/traslados',
meta: {
requiresAuth: true,
},
component: () => import('@/views/dashboard/Ventas/Transfers'),
},
{
name: 'Paseos',
path: 'ventas/paseos',
meta: {
requiresAuth: true,
},
component: () => import('@/views/dashboard/Ventas/Tours'),
},
{
name: 'Circuitos',
path: 'ventas/circuitos',
meta: {
requiresAuth: true,
},
component: () => import('@/views/dashboard/Ventas/Circuits'),
},
{
name: 'Perfil de Usuario',
path: 'pages/user',
meta: {
requiresAuth: true,
},
component: () => import('@/views/dashboard/pages/UserProfile'),
},
{
name: 'Administracion de usuarios',
path: 'configuracion/users',
meta: {
requiresAuth: true,
},
component: () => import('@/views/settings/usuarios/Usuarios'),
},
],
},
{
path: '/admon',
meta: {
requiresAuth: true,
},
component: () => import('@/views/settings/Index'),
children: [
{
name: 'Administración de Usuarios',
path: 'usuarios',
meta: {
requiresAuth: true,
},
component: () => import('@/views/settings/usuarios/Usuarios'),
},
{
name: 'Administración de grupos',
path: 'grupos',
meta: {
requiresAuth: true,
},
component: () => import('@/views/settings/usuarios/Grupos'),
},
{
name: 'Catálogo de Hoteles',
path: 'hoteles',
meta: {
requiresAuth: true,
},
component: () => import('@/views/settings/usuarios/Grupos'),
},
],
},
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes,
})
router.beforeEach((to, form, next) => {
const loggedIn = localStorage.getItem('user')
if (to.matched.some((record) => record.meta.requiresAuth) && !loggedIn) {
next('/pages/login')
} else {
next()
}
})
export default router
And this is the slice of code when I call "/admon/usuarios"
middleman(action) {
switch (action) {
case 'userProfile':
this.userProfile()
break
case 'userSettings':
this.$router.push('/admon/usuarios')
break
case 'userLogout':
this.userLogout()
break
}
}
The only way to go "/admon/usuarios" si refreshing the page, but the behavior is the same.
I will appreciate any help!
Thanks, Jose Rodriguez
to.matched.some((record) => record.meta.requiresAuth
instead of to.meta.requiresAuth? - Matheus Valenzato.matched
actually returns all nested path segments of the "to" route and I guess you don't want it. Please try withto.meta.requiresAuth
and tell me what happened - Matheus Valenza