React 路由
Posted xmforever
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了React 路由相关的知识,希望对你有一定的参考价值。
1. 下载插件
yarn add react-router-dom --save / npm i react-router-dom --save
2. 配置基础路由
import { BrowserRouter, Route, Link } from ‘react-router-dom‘ <Provider> <BrowserRouter> <div> <ul> <li> <Link to="/">一营</Link> </li> <li> <Link to="/Erying">二营</Link> </li> <li> <Link to="/Qibing">骑兵连</Link> </li> </ul> // exact用来精准匹配路由 <Route path="/" exact component={App} /> <Route path="/Erying" component={Erying} /> <Route path="/Qibing" component={Qibing} /> </div> </BrowserRouter> </Provider>
3. 重导向(Redirect)
import { Redirect } from ‘react-router-dom‘ <Redirect to="/somePath" ></Redirect> // 每次页面加载首先进这个地址
4. Switch匹配
import { Switch } from ‘react-router-dom‘ <Switch> <Route path="/" exact component={App} /> <Route path="/Erying" component={Erying} /> <Route path="/Qibing" component={Qibing} /> <Route component={NoMatch} /> // 没有匹配到任何地址, 404的情况 </Switch>
5. 子路由嵌套
<Route path="/" render={() => <Admin> <Switch> <Route path="/home" component={ Home } /> <Route path="/ui/buttons" component={ Buttons }></Route> <Route path="/form/login" component={ FormLogin }></Route> <Route path="/charts/line" component={ ChartLine }></Route> <Route path="/permission" component={ PermissionIndex }></Route> <Redirect to="/home" /> <Route component={ NoMatch } /> </Switch> </Admin> }/>
以上是关于React 路由的主要内容,如果未能解决你的问题,请参考以下文章
使用 Relay 和 React-Native 时的条件片段或嵌入的根容器
[react] Module not found: Can't resolve 'schedule' in 'C:Usersadcaldvmtn7myapp (代码片段