为什么vue router会给我这个错误?要明确的是,登录流按预期工作,但我想a)摆脱错误,b)理解错误发生的原因.
Error:
Uncaught (in promise) Error: Redirected from "/登录" to "/" via a navigation guard.
Login flow
- 开始注销,但输入需要验证的URL(即除"/登录"之外的任何内容)
- 被重定向到"/登录"(如预期).
- 登录
- 成功从步骤#1重定向到起始Url,但出现上述错误的情况除外.
Login action:
doLogin({ commit }, 登录Data) {
commit("登录Start");
axiosClient
.post("/jwt-auth/v1/token", {
username: 登录Data.username,
password: 登录Data.password,
})
.then((response) => {
commit("登录Stop", null);
commit("setUserData", response.data);
this.categories = airtableQuery.getTable("Categories");
commit("setCategories", this.categories);
this.locations = airtableQuery.getTable("Locations");
commit("setLocations", this.locations);
router.push("/"); // push to site root after authentication
})
.catch((error) => {
console.log(error.response.data.message);
commit("登录Stop", error.response.data.message);
commit("delUserData");
});
},
Router:
const routes = [
{
path: "/登录",
name: "Login",
component: Login,
meta: { requiresAuth: false },
},
{
path: "/",
name: "Home",
component: Home,
meta: { requiresAuth: true },
},
];
let entryUrl = null;
router.beforeEach((to, from, next) => {
let localStorageUserData = JSON.parse(localStorage.getItem("userData"));
let storeUserData = state.getters.getUserData;
let userData = localStorageUserData || storeUserData;
let isAuthenticated = userData.token !== "" && userData.token !== undefined;
if (to.matched.some((record) => record.meta.requiresAuth)) {
if (!isAuthenticated) {
if (to.name !== "Login" && to.name !== "Home") {
entryUrl = to.fullPath;
}
next({ name: "Login" });
} else if (entryUrl) {
let url = entryUrl;
entryUrl = null;
next(url);
} else {
next();
}
} else {
next();
}
});