How do I redirect to login if the path is '/' or '/login'?

Viewed 70

How do I redirect if the the path is '/' or '/login'. I was thinking of adding another object with '/' and component will still be login but I don't know if that's the right way. Here is my router.

const router = new Router({
    mode: 'history',
    linkExactActiveClass: 'active',
    routes: [
        {
            name: 'dashboard',
            path: '/dashboard',
            component: Dashboard,
            meta: {
                requiresAuth: true
            }
        },
        {
            name: 'profile',
            path: '/profile',
            component: Profile,
            meta: {
                requiresAuth: true
            }
        },
        {
            name: 'login',
            path: '/login',
            component: Login,
            meta: {
                requiresVisitor: true
            }
        },
        {
            name: 'logout',
            path: '/logout',
            component: Logout,
            meta: {
                requiresAuth: true
            }
        }
    ]
});
1 Answers

if you want to redirect with '/' or '/login' so you can try this one

const router = new Router({
    mode: 'history',
    linkExactActiveClass: 'active',
    routes: [
      {
        path: '',
        redirectTo: '/login',
        pathMatch: 'full'
      },
        {
            name: 'dashboard',
            path: '/dashboard',
            component: Dashboard,
            meta: {
                requiresAuth: true
            }
        },
        {
            name: 'profile',
            path: '/profile',
            component: Profile,
            meta: {
                requiresAuth: true
            }
        },
        {
            name: 'login',
            path: '/login',
            component: Login,
            meta: {
                requiresVisitor: true
            }
        },
        {
            name: 'logout',
            path: '/logout',
            component: Logout,
            meta: {
                requiresAuth: true
            }
        }
    ]
});
Related