在react-router4中,除了在路由文件中还能够在页面中写路由组件
dva初始化项目中router.js文件的内容以下react
import React from 'react'; import { Router, Route, Switch } from 'dva/router'; import IndexPage from './routes/IndexPage'; function RouterConfig({ history }) { return ( <Router history={history}> <Switch> <Route path="/" exact component={IndexPage} /> </Switch> </Router> ); } export default RouterConfig;
页面中的路由:web
import React from 'react'; import { Router, Route, Switch } from 'dva/router'; import IndexPage from './routes/IndexPage'; class Products extends React.Component { render() { return ( <div> <Switch> <Route path="/" component={IndexPage} /> </Switch> </div> ) } }
react-router-dom是用于DOM绑定的 React Router, 比react-router多出了<Link><BrowserRouter>这样的DOM类组件
import { Router, Route, Switch, Redirect } from 'react-router-dom';
// 用于导航的历史对象 <Router history={history}></Router> // 一个使用了 HTML5 history API 的高阶路由组件,保证你的 UI 界面和 URL 保持同步 <BrowserRouter basename="/minooo" // 添加一个基准URL forceRefresh={false} // 当浏览器不支持 HTML5 的 history API 时强制刷新页面 getUserConfirmation={getConfirmation()} // 导航到此页面前执行的函数 keyLength={12} // 设置它里面路由的 location.key 的长度。默认是6 ></BrowserRouter> <HashRouter></HashRouter> // Hash history 不支持 location.key 和 location.state。 // 另外因为该技术只是用来支持旧版浏览器,所以更推荐你们使用 BrowserRouter
若是你访问 /about,那么组件 About User Nomatch 都将被渲染出来,由于他们对应的路由与访问的地址 /about 匹配浏览器
<Route path="/about" component={About}/> <Route path="/:user" component={User}/> <Route component={NoMatch}/>
<Switch>只渲染出第一个与当前访问地址匹配的 <Route> 或 <Redirect>react-router
<Switch> <Route exact path="/" component={Home}/> <Route path="/about" component={About}/> <Route path="/:user" component={User}/> <Route component={NoMatch}/> </Switch>
<Route path="/" // url路径 exact // bool 严格匹配 ’/link’与’/’是不匹配的,可是在false的状况下它们是匹配的 component={IndexPage} // 渲染的组件 /> <Route path="/" // url路径 exact // bool 严格匹配 ’/link’与’/’是不匹配的,可是在false的状况下它们是匹配的 render={() => <div>Home</div>} // 内联渲染 />
// 经过from匹配路由重定向 <Switch> <Redirect from='/users/:id' to='/users/profile/:id'/> <Route path='/users/profile/:id' component={Profile}/> </Switch> // 经过render重定向 <Route exact path="/" render={() => ( loggedIn ? ( <Redirect to="/dashboard"/> ) : ( <PublicHomePage/> ) )}/>
// to为string <Link to='/courses?sort=name'/> // to为obj <Link to={{ pathname: '/courses', search: '?sort=name', hash: '#the-hash', state: { fromDashboard: true } }}/> // replace <Link to="/courses" replace /> // replace(bool):为 true 时,点击连接后将使用新地址替换掉访问历史记录里面的原地址; // 为 false 时,点击连接后将在原有访问历史记录的基础上添加一个新的纪录。默认为 false;
<NavLink to="/about" exact>About</NavLink> <NavLink to="/faq" activeClassName="selected" >FAQs</NavLink> <NavLink to="/faq" activeStyle={{ fontWeight: 'bold', color: 'red' }} >FAQs</NavLink> <NavLink to="/events/123" isActive={oddEvent} >Event 123</NavLink>
history 对象一般具备如下属性和方法:dom
location: object 当前访问地址信息组成的对象,具备以下属性:函数
pathname: string URL路径 search: string URL中的查询字符串 hash: string URL的 hash 片断 state: string 例如执行 push(path, state) 操做时,location 的 state 将被提供到堆栈信息里,state 只有在 browser 和 memory history 有效。
location 是指你当前的位置,将要去的位置,或是以前所在的位置this
{ key: 'ac3df4', // not with HashHistory! pathname: '/somewhere' search: '?some=search-string', hash: '#howdy', state: { [userDefined]: true } }
在如下情境中能够获取 location 对象url
能够在不一样情境中使用 location:3d
<Link to={location} /> <NaviveLink to={location} /> <Redirect to={location /> history.push(location) history.replace(location)
const Topics = ({ match }) => ( <div> <Link to={`${match.url}/rendering`}>Rendering with React</Link> <Route path={`${match.url}/:topicId`} component={Topic} /> </div> );
match 对象包含了 <Route path> 如何与 URL 匹配的信息,具备如下属性:指针
在如下情境中能够获取 match 对象
当一个 Route 没有 path 时,它会匹配一切路径。
具体实例可参考 https://reacttraining.com/rea...