javascript - VueJS 2, router guard - Stack Overflow

My index route is defined as: { path: '', adminOnly: false, ponent: Main },Is there a way to

My index route is defined as:

{ path: '/', adminOnly: false, ponent: Main },

Is there a way to access the 'adminOnly' property?

There seems to be no way of doing so in this block of code:

routes.beforeEach((to, from, next) => {
    console.log(to)
    next();
});

My routes file:

import Vue from 'vue';
import VueRouter from 'vue-router';

import Main from '../ponents/Main.vue';
import About from '../ponents/About.vue';

const NotFoundException = {
    template: '<div>Route Was Not Found</div>'
};

Vue.use(VueRouter);

const routes = new VueRouter({
    mode: 'history',
    hashbang: false,
    linkActiveClass: 'active',
    base: '/jokes',
    routes: [
        { path: '/', adminOnly: false, ponent: Main },
        { path: '/about', adminOnly: false, ponent: About },
        { path: '*', adminOnly: false, ponent: NotFoundException }
    ]
});
routes.mode = 'html5';

routes.beforeEach((to, from, next) => {
    // CHECK IF ADMINONLY EXISTS
    next();
});

export default routes;

I did get a solution by adding a mixin of adminOnly.js which has the following code in it: But then again, the mixin has to be added to each ponent if I was to redirect the user to the login page if not admin. //Just a test var isAdmin = false;

export default {
        beforeRouteEnter (to, from, next) {
            if(!isAdmin) {
                next((vm) => {
                   vm.$router.push('/login');
                });
            }
        }
    }

My index route is defined as:

{ path: '/', adminOnly: false, ponent: Main },

Is there a way to access the 'adminOnly' property?

There seems to be no way of doing so in this block of code:

routes.beforeEach((to, from, next) => {
    console.log(to)
    next();
});

My routes file:

import Vue from 'vue';
import VueRouter from 'vue-router';

import Main from '../ponents/Main.vue';
import About from '../ponents/About.vue';

const NotFoundException = {
    template: '<div>Route Was Not Found</div>'
};

Vue.use(VueRouter);

const routes = new VueRouter({
    mode: 'history',
    hashbang: false,
    linkActiveClass: 'active',
    base: '/jokes',
    routes: [
        { path: '/', adminOnly: false, ponent: Main },
        { path: '/about', adminOnly: false, ponent: About },
        { path: '*', adminOnly: false, ponent: NotFoundException }
    ]
});
routes.mode = 'html5';

routes.beforeEach((to, from, next) => {
    // CHECK IF ADMINONLY EXISTS
    next();
});

export default routes;

I did get a solution by adding a mixin of adminOnly.js which has the following code in it: But then again, the mixin has to be added to each ponent if I was to redirect the user to the login page if not admin. //Just a test var isAdmin = false;

export default {
        beforeRouteEnter (to, from, next) {
            if(!isAdmin) {
                next((vm) => {
                   vm.$router.push('/login');
                });
            }
        }
    }
Share Improve this question asked Jan 10, 2017 at 1:34 Paul DiamantPaul Diamant 1893 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

Yes there is better way to handle this. Every route object can have meta field. Just wrap adminOnly property in meta object:

 routes: [
        { path: '/', meta: { adminOnly: false }, ponent: Main },
        { path: '/about', meta: { adminOnly: false }, ponent: About },
        { path: '*', meta: { adminOnly: false }, ponent: NotFoundException }
    ]

Check route for admin rights:

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.adminOnly)) {
    // this route requires auth, check if logged in
    // if not, redirect to login page.
  }
}

For more please check docs and I created little example on jsFiddle

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1741232749a4330697.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信