英文:
Maximum call stack size exceeded vue 2
问题
我在尝试根据用户的roles_id值将用户重定向到其路径时遇到了这个问题
const getUserRole = (to, from, next) => {
const currentUser = JSON.parse(localStorage.getItem("userData"));
if (currentUser) {
if (currentUser.roles_id === 1 && to.name === "user-dashboard") {
next({
name: "dashboard-new"
});
} else if (currentUser.roles_id === 2 && to.name === "dashboard-new") {
next({
name: "user-dashboard"
});
} else if (currentUser.roles_id === 3 && to.name === "mails-track") {
next({
name: "dispatch-dashboard"
});
} else if (to.name === "dashboard-new" && currentUser.roles_id === 1) {
next();
} else if (to.name === "user-dashboard" && currentUser.roles_id === 2) {
next();
} else if (to.name === "dispatch-dashboard" && currentUser.roles_id === 3) {
next();
} else {
next("/");
}
} else {
next("/");
}
};
export default [
{
path: "/dashboard",
name: "dashboard-new",
component: () =>
import("@/views/dashboard/new/DashboardNew.vue"),
meta: {
requiresAuth: true
},
beforeEnter: (to, from, next) => {
const currentUser = JSON.parse(localStorage.getItem("userData"));
if (currentUser && currentUser.roles_id === 1) {
getUserRole(to, from, next);
} else {
next("/");
}
}
},
{
path: "/user-dashboard",
name: "user-dashboard",
component: () =>
import("@/views/dashboard/new/user/UserDashboard.vue"),
meta: {
requiresAuth: true
},
beforeEnter: (to, from, next) => {
const currentUser = JSON.parse(localStorage.getItem("userData"));
if (currentUser && currentUser.roles_id === 2) {
getUserRole(to, from, next);
} else {
next("/");
}
}
}
];
但是我得到了"Maximum call stack size exceeded"的错误,我尝试使用switch条件也不起作用。
在本地环境上运行正常,但一旦上传到主机上就出现了这个问题。
我的代码有什么问题吗?
英文:
I am facing this problem when i try to redirect the user to their path depends on their roles_id value
<!-- language: lang-js -->
const getUserRole = (to, from, next) => {
const currentUser = JSON.parse(localStorage.getItem("userData"));
if (currentUser) {
if (currentUser.roles_id === 1 && to.name === "user-dashboard") {
next({
name: "dashboard-new"
});
} else if (currentUser.roles_id === 2 && to.name === "dashboard-new") {
next({
name: "user-dashboard"
});
} else if (currentUser.roles_id === 3 && to.name === "mails-track") {
next({
name: "dispatch-dashboard"
});
} else if (to.name === "dashboard-new" && currentUser.roles_id === 1) {
next();
} else if (to.name === "user-dashboard" && currentUser.roles_id === 2) {
next();
} else if (to.name === "dispatch-dashboard" && currentUser.roles_id === 3) {
next();
} else {
next("/");
}
} else {
next("/");
}
};
export default [{
path: "/dashboard",
name: "dashboard-new",
component: () =>
import ("@/views/dashboard/new/DashboardNew.vue"),
meta: {
requiresAuth: true
},
beforeEnter: (to, from, next) => {
const currentUser = JSON.parse(localStorage.getItem("userData"));
if (currentUser && currentUser.roles_id === 1) {
getUserRole(to, from, next);
} else {
next("/");
}
}
},
{
path: "/user-dashboard",
name: "user-dashboard",
component: () =>
import ("@/views/dashboard/new/user/UserDashboard.vue"),
meta: {
requiresAuth: true
},
beforeEnter: (to, from, next) => {
const currentUser = JSON.parse(localStorage.getItem("userData"));
if (currentUser && currentUser.roles_id === 2) {
getUserRole(to, from, next);
} else {
next("/");
}
}
}
];
But I am getting "Maximum call stack size exceeded" i tried to use switch condition and also didn't work
It is working fine on local but once i upload it to my host it show this problem
Is there anything wrong with my code?
答案1
得分: 0
在调试之后,我发现在本地环境中,currentUser.roles_id === 1
会返回数字1,但在主机环境中会返回字符串。
所以在这种情况下,我不需要指定'==='中的类型,所以我将其更改为'==',只检查值。
英文:
After a debugging i found that in local currentUser.roles_id === 1
will return 1 as number but in host will return it as string
so in this case i don't need to specify the type in '===' so i changed it to '==' to check only the value
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论