VueJS 中的路由验证

Validation of routes in VueJS

我正在尝试为某些用户制作 ceratin 路线,以便每个用户都不会看到相同的视图。例如,如果用户以管理员身份登录,他将直接转到 '/admin' 而不是 '/home'。 我的成就就在这里

index.js - 路由文件

{
    path: '/',
    name: 'Home',
    component: Home,
    meta: {
      requiresAuth: true,
      user:'normal-user'
    },
    beforeEnter: (to, from, next) => {
      console.log(from)
      if(from.path == '/admin' || from.path == '/forms' || from.path == '/steps') {
        next('/admin')
      }
      else 
        next()
    }
  }

请记住,我有一条 /forms 路线和 /steps

我想要实现的是,如果用户来自 '/admin' 路线,当他试图去 '/' 时,他将 're-directed' 到 '/admin' .

我设法做到了这一点,但我知道这不是实施它的最佳方式。有什么建议吗?

听起来你想要一个导航守卫:

https://router.vuejs.org/guide/advanced/navigation-guards.html

router.beforeEach((to, from, next) => {
    if (from.path === 'admin' && to.path === '/') {
        next('/admin');
    } else {
        next();
    }
});