react中的路由--react-router-dom

一、进入到项目目录,打开命令行,安装react-router-dom,指令:npm install react-router-dom --save-devreact

二、在一个js文件中引入react-router-dom, import { HashRouter, Route, Switch } from 'react-router-dom'npm

三、在render函数中return路由结构,以下:react-router

<HashRouter>
    <Switch>
        <Route path='/' component={ Home } /> 
        <Route path='/home' component={ Home } /> 
        <Route path='/search' component={ Search } /> 
        <Route path='/mine' component={ Mine } /> 
    </Switch>
</HashRouter>

四、将这个js文件暴露出去,在index.js中引入,以后在index.js中ReactDOM.render(<Router/>,document.getElementById('root')); 至此,当咱们在地址栏中输入home、search或mine时,页面都会跳到其路由对应的组件dom

五、以函数的形式进行路由跳转,在a标签上绑定onClick事件,经过this.props.history.push这个函数跳转函数

相关文章
相关标签/搜索