Closed NieShiyi closed 1 year ago
这是 history.block(() => {})
中回调函数的类型,返回值是 void
,没有你说的功能:
/**
* A function that receives transitions when navigation is blocked.
*/
export interface Blocker {
(tx: Transition): void;
}
参照这个文档 https://github.com/remix-run/history/blob/main/docs/blocking-transitions.md
useEffect(() => {
const unblock = history.block(({ location , retry}) => {
// 想要路由不被拦截,先取消监听,在调用retry方法
// unblock();
// retry();
// 时长这样使用
if(!window.confirm("是否离开该页面?")){
console.log('no')
}else{
unblock();
retry();
}
});
return () => {
unblock?.();
};
}, []);
What happens?
history.block的回调函数不管有没有返回值或是返回任何值,都会阻止路由跳转。按理说teturn true应该是允许正常跳转的,但是现在还是被阻止了,都无法正常跳转页面。
Mini Showcase Repository(REQUIRED)
https://github.com/NieShiyi/umi4-antdpro-style/tree/umi-history-block
How To Reproduce
Steps to reproduce the behavior: 1. 2. 点击左侧的菜单,发现跳转被阻止了 Expected behavior 1. 2. 期望history.block的回调函数里返回true时,可以正常跳转
Context
1.随便在一个页面函数中写如下代码
2.随意点击菜单跳转,发现无法跳转