问题描述
当用户转到应用程序中的特定页面时,我尝试通过遵循特定链接来重定向用户。在这种情况下,浏览器以前没有历史记录,当用户单击浏览器的后退按钮时,我尝试将用户重定向到应用程序的主页(默认情况下,他们将返回到浏览器的主页)。
这是我的代码:
componentDidMount() {
if (!location.state) {
window.history.pushState(location.state ? { catalog: true } : null,null,window.location.pathname);
}
window.addEventListener('popstate',this.onBackButtonEvent);
}
componentWillUnmount() {
window.removeEventListener('popstate',this.onBackButtonEvent);
}
onBackButtonEvent = (e) => {
e.preventDefault();
this.back();
}
back = () => {
const { history,location } = this.props;
if (location.state) {
history.goBack();
}
else {
history.push('/home');
}
}
如果有人通过以下链接直接跳转到页面,则this.props.location.state 将为空。
问题是,有时它可以工作,但有时它却不工作,大多数情况下它可以在Mozilla中工作,但有时在chrome中不工作。有人可以帮助我找出代码中的错误吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)