我正在电子中创建我的第一个反应应用程序(也是我的第一个电子应用程序)。我有两条路线,需要从一条导航到另一条。为此,我使用以下代码:
根
ReactDOM.render( <Router history={browserHistory}> <App /> </Router>, document.getElementById('root') );
应用程式
class App extends React.Component { constructor() { super(); } render() { return ( <div className="app-master"> <Switch> <Route path='/city' component={CityList}/> <Route path='/' component={SplashScreen}/> </Switch> </div> ) } }
页
import { browserHistory } from 'react-router'; ... browserHistory.push('/city');
这行给出了错误,
TypeError: Cannot read property 'push' of undefined
我在网上搜索了可能的解决方案,但找不到!SO上也有许多类似的问题,但是对我来说都没有作用:(
您现在必须从历史记录模块中导入它,该模块提供了3种不同的方法来创建不同的历史记录。
createBrowserHistory
createMemoryHistory
createHashHistory
您不能在电子环境中使用浏览器历史记录,而不能使用哈希或内存。
import { createHashHistory } from 'history' const history = createHashHistory()
然后,您可以使用history注入的道具
history
this.props.history.push('/')