如何使用 Electron 中的“react-router”从组件外部路由?
How to route from outside of components with `react-router` in Electron?
我在 Electorn 中遇到 react-router
的问题是我不能做类似 window.location = '/'
的事情,因为电子应用程序中的 window.location
指向 main 的文件系统级路径脚本。
我知道react-router
的官方文档说要这样做
// history.js
import createBrowserHistory from 'history/lib/createBrowserHistory'
export default createBrowserHistory()
// index.js
import history from './history'
render(<Router history={history}/>, el)
// actions.js
import history from './history'
history.replaceState(null, '/some/path')
但是调用 history.replaceState()
在我的应用程序中没有任何作用。
最可疑和奇怪的部分是,虽然我使用 createBrowserHistory
作为我的路由器历史记录 window.location
打印散列路径。
基于历史的路由是否存在任何 Electron 特定问题?如果不是,为什么调用 history.replaceState(null, '/')
不会更改路由?
从 react-router 1.0.3 开始使用哈希历史:
// history.js
import createHashHistory from 'history/lib/createHashHistory';
export default createHashHistory();
渲染:
// index.js
import history from './history'
render(<Router history={ history }/>, el);
路线如下:
// action.js
import history from './history';
history.replaceState(null, '/some/path');
阅读 doc 了解详情。
我在 Electorn 中遇到 react-router
的问题是我不能做类似 window.location = '/'
的事情,因为电子应用程序中的 window.location
指向 main 的文件系统级路径脚本。
我知道react-router
的官方文档说要这样做
// history.js
import createBrowserHistory from 'history/lib/createBrowserHistory'
export default createBrowserHistory()
// index.js
import history from './history'
render(<Router history={history}/>, el)
// actions.js
import history from './history'
history.replaceState(null, '/some/path')
但是调用 history.replaceState()
在我的应用程序中没有任何作用。
最可疑和奇怪的部分是,虽然我使用 createBrowserHistory
作为我的路由器历史记录 window.location
打印散列路径。
基于历史的路由是否存在任何 Electron 特定问题?如果不是,为什么调用 history.replaceState(null, '/')
不会更改路由?
从 react-router 1.0.3 开始使用哈希历史:
// history.js
import createHashHistory from 'history/lib/createHashHistory';
export default createHashHistory();
渲染:
// index.js
import history from './history'
render(<Router history={ history }/>, el);
路线如下:
// action.js
import history from './history';
history.replaceState(null, '/some/path');
阅读 doc 了解详情。