我在 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
时,一切正常。问题是在双重条件下 ||
。
尝试使用异步方法
beforeEnter: async (to, from, next) => {
await storeInit
.then(() => {
const admin = store.getters.userIsAdmin;
const userHasPermission = store.getters.userIsAbsencesAdmin;
if (!admin || !userHasPermission) {
next('/');
}
else next();
});
},