最近调试uniapp的拦截器非常头晕,使用白名单拦截时时常发生界面被锁定(lock)而跳转失败的情况,现在的修改方案是将所有return true和return false全部改为了return e即可正常拦截跳转。
【上述问题是在app调用tabbar页面时发生,h5暂且正常】
对此我提出的问题是,文档中提及的return true和false是什么逻辑什么作用,使用return e替换会有什么隐藏隐患吗,如果有,我应该如何合理运用true 和 false,因为目前为止会一直不规律的拦截失败、跳转锁定的情况发生。
附上修改后的部分代码
复制代码let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"];
list.forEach(item => { //用遍历的方式分别为,uni.navigateTo,uni.redirectTo,uni.reLaunch,uni.switchTab这4个路由方法添加拦截器
uni.addInterceptor(item, {
//注意 这个里面的回调函数不可以使用 async 类型的
invoke(e) {
const token = uni.getStorageSync('uni_id_token');
let islogin = (token && token.length >2) ? true : false;
if(!islogin){
console.log("未登录状态下拦截 跳转到登陆页:");
console.log('e',e);
console.log('checkPages',checkPages(e.url));
if(!checkPages(e.url)){
utils.GoToPage(PAGE_ENUM.LOGIN_PAGE);
console.log('false false false', e.url);
return e;
}
console.log('ok');
}
return e;
},
fail(err) { // 失败回调拦截
console.log('这里',err);
}
})
})
0 个回复