this.props
为空集合,找不到history
和location
?
第一种解决方法
给子组件传 tihs.props
import React from 'react'
import { Redirect, Route, Switch } from 'react-router-dom'
// Home Category 都是组件
export default class Admin extends React.Component {
render(){
const props = this.props
return(
<Switch>
{/* 传值 */}
<Route path='/' {...props} exact component={Home} />
<Route path='/category' component={Category} />
</Switch>
)
}
}
第二种解决方法
用 withRouter
包装给需要的组件
import React from 'react'
import { withRouter } from 'react-router-dom'
class Home extends React.Component {
render(){
console.log(this.props)
return(
<>
<div>我是Home组件</div>
</>
)
}
}
export default withRouter(Home)