Vuex 和 Vue Router - beforeEnter 上的双重条件不起作用

问题描述 投票:0回答:1

我在 beforeEnter 中有双重条件,没有人可以进入正确的位置。有条件限制什么的吗? :(

我在路由器文件中的代码:

const routes = {
    path: `${routerConfig.preRoute}/absence`,
    beforeEnter: (to, from, next) => {
        storeInit
            .then(() => {
                const admin = store.getters.userIsAdmin;
                const userHasPermission = store.getters.userIsAbsencesAdmin;
                if (!admin || !userHasPermission) {
                    next('/');
                }
                else next();
            });
    },

我在商店文件中的代码:

getters: {
        userIsAdmin: (state, getters) => {
            return state.userPermissions.some(permission => +permission === permissionsArray.ADMIN_PLATFORM);
        },
        userIsGoalAdmin: (state, getters) => {
            return state.userPermissions.some(permission => +permission === permissionsArray.ADMIN_GOAL);
        },
        userIsAbsencesAdmin: (state, getters) => {
            // return state.userPermissions.some(permission => +permission === permissionsArray.ADMIN_ABSENCES || +permission === permissionsArray.ADMIN_PLATFORM);
            return state.userPermissions.some(permission => +permission === permissionsArray.ADMIN_ABSENCES);
        },
        user: (state, getters) => {
            return state.user;
        },
    },

我需要

admin
userHasPermission
输入到 next(),但每个人都被重定向到 next('/')。 当条件为
!admin
!userHasPermission
时,一切正常。问题是在双重条件下
||

vue.js vuejs2 vuex vue-router
1个回答
0
投票

尝试使用异步方法

beforeEnter: async (to, from, next) => {
    await storeInit
        .then(() => {
            const admin = store.getters.userIsAdmin;
            const userHasPermission = store.getters.userIsAbsencesAdmin;
            if (!admin || !userHasPermission) {
                next('/');
            }
            else next();
        });
},
© www.soinside.com 2019 - 2024. All rights reserved.