我有一个导入路由器的组件,它无法正常工作,并且没有任何错误消息。页面没有出现在浏览器中。这是一个课程的应用程序。由于该课程有些古老,因此有些事情可能会过时。这是我组件的代码:
import '../common/template/dependencies'
import React from 'react'
import Routes from './Routes'
export default props => (
<div className='wrapper'>
<div className='content-wrapper'>
<Routes />
</div>
</div>
)
执行路由的组件Routes.js:
import React from 'react'
import {Router, Route , Redirect, hasHistory} from 'react-router'
import Dashboard from '../dashboard/Dashboard'
import BillingCycle from '../billingCycle/BillingCycle'
export default props => (
<Router history={hasHistory}>
<Route path='/' component={Dashboard} />
<Route path='/billingCycles' component={BillingCycle} />
<Redirect from='*' to='/' />
</Router>
)
当我在上面的组件的这一行注释时,一切正常。
{/*<Routes />*/}
答案 0 :(得分:2)
import React from 'react'
import {BrowserRouter as Router, Route, Redirect, Switch} from 'react-router-dom';
import Dashboard from './DashBoard';
import BillingCycle from './BillingCycle'
export default props => (
<Router>
<Switch>
<Route exact path='/' component={Dashboard}/>
<Route exact path='/billingCycles' component={BillingCycle}/>
<Redirect from='*' to='/'/>
</Switch>
</Router>
)
检查代码是否具有hasHistory在新版本的react-router中可用。 另外,您应该使用react-router-dom。 希望对您有所帮助。
答案 1 :(得分:0)
使用react-router-dom
按以下方式更改您在Route.js中的导入:
import {BrowserRouter as Router, Route} from 'react-router-dom'