Vue.js vue路由器&x2014;未捕获(承诺中)错误:重定向自/“登录”;至/&引用;通过导航装置
为什么vue路由器会给我这个错误?要明确的是,登录流按预期工作,但我想a)摆脱错误,b)理解错误发生的原因 错误:Vue.js vue路由器&x2014;未捕获(承诺中)错误:重定向自/“登录”;至/&引用;通过导航装置,vue.js,promise,vue-router,Vue.js,Promise,Vue Router,为什么vue路由器会给我这个错误?要明确的是,登录流按预期工作,但我想a)摆脱错误,b)理解错误发生的原因 错误: Uncaught(in promise)错误:通过导航保护从“/login”重定向到“/”。 登录流 开始注销,但输入需要验证的URL(即除“/login”之外的任何内容) 被重定向到“/登录”(如预期) 登录 成功从步骤#1重定向到起始Url,但出现上述错误的情况除外 登录操作: doLogin({commit},loginda){ 提交(“登录启动”); axiosClient
Uncaught(in promise)错误:通过导航保护从“/login”重定向到“/”。
登录流
doLogin({commit},loginda){
提交(“登录启动”);
axiosClient
.post(“/jwt auth/v1/token”{
用户名:loginda.username,
密码:loginda.password,
})
。然后((响应)=>{
提交(“loginStop”,null);
提交(“setUserData”,response.data);
this.categories=airtableQuery.getTable(“categories”);
提交(“setCategories”,this.categories);
this.locations=airtableQuery.getTable(“位置”);
提交(“setLocations”,this.locations);
router.push(“/”;//身份验证后推送到站点根目录
})
.catch((错误)=>{
日志(错误、响应、数据、消息);
提交(“loginStop”,error.response.data.message);
提交(“delUserData”);
});
},
路由器:
const路由=[
{
路径:“/login”,
名称:“登录”,
组件:登录,
meta:{requireAuth:false},
},
{
路径:“/”,
姓名:“家”,
组成部分:家庭,
meta:{requireAuth:true},
},
];
让entryUrl=null;
路由器.beforeach((到、从、下一个)=>{
让localStorageUserData=JSON.parse(localStorage.getItem(“userData”);
让storeUserData=state.getters.getUserData;
让userData=localStorageUserData | | storeUserData;
让isAuthenticated=userData.token!==“”&&userData.token!==未定义;
if(to.matched.some((记录)=>record.meta.requireAuth)){
如果(!已验证){
如果(to.name!=“登录”&&to.name!=“主页”){
entryUrl=to.fullPath;
}
下一步({name:“Login”});
}else if(entryUrl){
让url=entryUrl;
entryUrl=null;
下一步(url);
}否则{
next();
}
}否则{
next();
}
});
下一版本的vue路由器将更新错误消息。错误将显示为:
通过导航保护从“/login”转到“/”时重定向
在代码的某个地方,在重定向到“/登录”后,您正在重定向回“/”。vue路由器正在抱怨。您需要确保每个导航操作只有一个重定向。如果您的重定向是在调用router.push('/someroute)之后进行的,那么您可以作为router捕获此错误。push()是一个承诺,您可以如下所示附加一个捕获
$router.push(“/somesecureroute”)
.catch(错误=>{
控制台信息(错误消息)
})
我也有同样的问题,我认为这是配置问题,但事实并非如此
你可以试试这个代码
async-doLogin({commit,dispatch},loginda){
提交(“登录启动”);
let response=等待axiosClient
.post(“/jwt auth/v1/token”{
用户名:loginda.username,
密码:loginda.password,
})
返回DISACTH('尝试',响应)
}
异步尝试({commit},响应){
试一试{
提交(“loginStop”,null);
提交(“setUserData”,response.data);
this.categories=airtableQuery.getTable(“categories”);
提交(“setCategories”,this.categories);
this.locations=airtableQuery.getTable(“位置”);
提交(“setLocations”,this.locations);
}
捕获((错误)=>{
日志(错误、响应、数据、消息);
提交(“loginStop”,error.response.data.message);
提交(“delUserData”);
})
}
在调用多洛金作用的组件中
this.doLogin(this.loginda)
.然后(()=>{
此。$router.replace(“/”)
.catch(错误=>{
console.log(错误)
})
})
.catch(e=>{
控制台日志(e)
})
我有一个类似的错误,但是对于.beforeach中的入职重定向,通过替换.beforeach条件逻辑解决了该错误:
next({name:“Onboarding”});
与
router.push({path:'Onboarding'});
此错误旨在通知调用方,$router.push
导航未到达最初预定的位置。如果希望重定向,可以使用以下代码安全地忽略错误
从“vue路由器”导入VueRouter
const{isNavigationFailure,NavigationFailureType}=VueRouter
...
这是。$router.push(“/”)
.catch((e)=>{
如果(!isNavigationFailure(e,NavigationFailureType.redirected)){
承诺.拒绝(e)
}
}
有关此问题的讨论,请参阅。我也有相同的错误。此错误由路由器.push(“/”;
行创建-它试图告诉您,向主页的推送被导航卫士中的重定向中断。
但事实上,这不是一个错误,因为这是一个预期的行为
我通过以下方式忽略了这些错误:
const router = new VueRouter({
mode: 'history',
routes: _routes,
});
/**
* Do not throw an exception if push is rejected by redirection from navigation guard
*/
const originalPush = router.push;
router.push = function push(location, onResolve, onReject) {
if (onResolve || onReject) {
return originalPush.call(this, location, onResolve, onReject);
}
return originalPush.call(this, location).catch((err) => {
let reg = new RegExp('^Redirected when going from "[a-z_.\\/]+" to "[a-z_.\\/]+" via a navigation guard.$');
if (reg.test(err.message)) {
// If pushing interrupted because of redirection from navigation guard - ignore it.
return Promise.resolve(false);
}
// Otherwise throw error
return Promise.reject(err);
});
};
我花了几个小时调试这个问题,并针对丑陋的Uncaught(in promise)错误得到了以下结果:从…
错误开始时重定向
请注意,错误不是针对“重定向”,而是针对第一次导航的初始调用方
这是故意的,为什么?
阅读
TL;DR:假设您在A页上,点击一个按钮将您带到B页(有点像A页上的method:goToB(){router.push('/B');}
)
这个错误是让goToB(