【问题标题】:Validation of routes in VueJSVueJS 中的路由验证
【发布时间】:2021-01-17 12:35:56
【问题描述】:

我正在尝试为某些用户制作某些路线,以便每个用户都看不到相同的视图。例如,如果用户以管理员身份登录,他将直接转到'/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' 路由,当他尝试转到“/”时,他将被“重定向”到'/admin'

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

【问题讨论】:

    标签: javascript vue.js vue-router router


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2018-08-28
      • 1970-01-01
      • 2019-07-02
      • 2021-08-17
      • 1970-01-01
      • 2018-01-18
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      相关资源
      最近更新 更多