vue.js - 如果路径为 '/' 或 '/login' ,如何重定向登录?

标签 vue.js vue-router

如果路径是“/”或“/login”,如何重定向。我正在考虑添加另一个带有“/”的对象,并且组件仍然可以登录,但我不知道这是否是正确的方法。 这是我的路由器。

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
            }
        }
    ]
});

最佳答案

如果您想使用“/”或“/login”进行重定向,可以尝试这个

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
            }
        }
    ]
});

关于vue.js - 如果路径为 '/' 或 '/login' ,如何重定向登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58369954/

相关文章:

javascript - Vue.js 缓存方法结果?

javascript - Vue 在输入时搜索 Ajax 响应数组

vue.js - 如何删除在 Vue 指令中使用 vnode.context.$on 创建的事件?

Vue.JS - 'history' 和 'hash' 路由器?

vue.js - Vuejs 'beforeunload' 事件未按预期触发

node.js - 部署Nuxt App后,baseURL是否仍为localhost?

javascript - 根据输入的最大长度更新进度条

vue.js - VueJS 基于角色的路由器身份验证

vue.js 路由器 NavigationDuplicated 用于查询更改

vue-router - Vue Router - 捕获所有通配符不起作用