我有注册页面,当用户输入一些细节,并点击刷新或后退/前进button.Then,我们必须提醒用户与模式弹出喜欢:“你确定要离开页面”。我已经尝试了一些,但警报显示在每个页面。我只需要在注册页面,而不需要刷新其他页面的警报消息。
我已将此代码添加到注册页面
componentWillMount()
{
var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload';
myEvent(chkevent, function(e) {
var confirmationMessage = 'Are you sure to leave the page?';
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
});
}
如何防止除注册页面以外的其他页面刷新预警信息,以及如何在刷新/后退/前进按钮中添加自定义预警信息。
发布于 2018-05-21 20:47:04
您可以在react-router-dom中使用提示符,可以在react-router-dom文档https://reacttraining.com/react-router/core/api/Prompt中找到
该对象有一个路径名属性,它是下一个路径。通过检查它,您可以确定该路径是否安全。这就是我要说的:
<Prompt message={(params) =>
params.pathname == '/about' ? "Move away?" : true } />
https://stackoverflow.com/questions/50448661
复制相似问题