最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Guarding child routes in vuejs - Stack Overflow

programmeradmin4浏览0评论

Am trying to guard child routes from the parent routes but this fails

    export default new Router({
    routes: [
    //frontend routes
    {
      {path: 'auth', ponent: Auth, children: authroutes,
        beforeEnter: (to, from, next) => {
        // check if user is a guest or loggedin
        auth.canAccess(permissions.is_guest)
          .then((res) => {
            if (res.data.status) {
              next();
            } else {
              router.push('/auth/not-allowed');
            }
          })
       }}
      ]
     }
   ]
 })

Now on my child routes

authroutes.js

const authroutes = [
  {path: '', redirect: 'login'},
  {path: 'login', ponent: Login, name: "login" },
];

But when i place the above beforeenter on the child routes it works but it leads to alot of code duplication.

How can i guard children from the parent route

eg: guard /auth/login and /auth/register

Am trying to guard child routes from the parent routes but this fails

    export default new Router({
    routes: [
    //frontend routes
    {
      {path: 'auth', ponent: Auth, children: authroutes,
        beforeEnter: (to, from, next) => {
        // check if user is a guest or loggedin
        auth.canAccess(permissions.is_guest)
          .then((res) => {
            if (res.data.status) {
              next();
            } else {
              router.push('/auth/not-allowed');
            }
          })
       }}
      ]
     }
   ]
 })

Now on my child routes

authroutes.js

const authroutes = [
  {path: '', redirect: 'login'},
  {path: 'login', ponent: Login, name: "login" },
];

But when i place the above beforeenter on the child routes it works but it leads to alot of code duplication.

How can i guard children from the parent route

eg: guard /auth/login and /auth/register

Share Improve this question edited Nov 25, 2017 at 12:00 Roshna Omer 7211 gold badge13 silver badges21 bronze badges asked Nov 25, 2017 at 11:43 GeoffGeoff 6,66924 gold badges99 silver badges214 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Just use route's meta field for the fields you want to guard like this:

const authroutes = [
    {path: '', redirect: 'login', meta: {requiresAuth: true}},
    {path: 'login', ponent: Login, name: "login" , meta: {requiresAuth: true}},,
];

then configure your router to check if the route has a specified meta field and perform your redirect logic

router.beforeEach((to, from, next) => { 
    if (to.matched.some(record => record.meta.requiresAuth)) { 
        auth.canAccess(permissions.is_guest)
              .then((res) => {
                if (res.data.status) {
                      next();
                } else {
                     next('/auth/not-allowed');
                }
              })
    }else { 
        next() // make sure to always call next()! 
    } 
});

Check out more info here: Route meta fields

发布评论

评论列表(0)

  1. 暂无评论