Javascript 无法重定向到'/';更换为单钩式
我有两种方法:Javascript 无法重定向到'/';更换为单钩式,javascript,reactjs,single-page-application,react-router,Javascript,Reactjs,Single Page Application,React Router,我有两种方法: toLogin: function (nextState, replace) { if (!localStorage.get("token")) { return replace('/signin'); } }, toHome: function (nextState, replace) { if (localStorage.get("token")) { return replace('/'); } }
toLogin: function (nextState, replace) {
if (!localStorage.get("token")) {
return replace('/signin');
}
},
toHome: function (nextState, replace) {
if (localStorage.get("token")) {
return replace('/');
}
}
return replace('/signin')
工作正常,但是return replace('/')
给了我一个错误:
Uncaught SecurityError:未能对“History”执行“replaceState”:无法在原始文档中创建URL为“http:”的历史状态对象http://localhost:3000“。
我是否需要设置一些我在文档中遗漏的基本URL内容,或者这是一个bug?您的代码或React正在尝试插入
http://...
URL插入导致此安全冲突的历史记录(这就是错误的含义)
pushState
的URL参数应与当前页面相关,或与您自己域中的绝对URL相关。您不能跨域推送状态-这将是一个主要的安全漏洞
报告说:
新历史记录条目的URL由此参数给定。请注意,在调用pushState()之后,浏览器不会尝试加载此URL,但稍后可能会尝试加载此URL,例如在用户重新启动浏览器之后。新的URL不需要是绝对的;如果是相对的,则相对于当前URL解析。新URL必须与当前URL的来源相同;否则,pushState()将引发异常。此参数是可选的;如果未指定,则将其设置为文档的当前URL
我自己就试过了,但我没有收到这个错误。你能用一个很小的例子重现它吗?