react路由,4.x的差异还是比较大,暂时还是3.x的版本

安装:

npm install -S react-router@3.x

配置:

import { Router, Route, hashHistory } from 'react-router';

<Router history={hashHistory}>{/*browserHistory、hashHistory、createMemoryHistory*/}
<Route exact path="/" component={App}> {/*exact 表示精确匹配*/}
<IndexRoute component={Home}/>{/*显式指定Home是根路由的子组件,即指定默认情况下加载的子组件。*/}
<IndexRedirect to="/welcome" />{/*IndexRedirect组件用于访问根路由的时候,将用户重定向到某个子组件。*/}
<Route path="welcome" component={Welcome} />
<Route path="/repos" component={Repos}/>
<Route path="/about/(:id)" component={About}/>
<Route path="inbox" component={Inbox}>
{/*<Redirect>组件用于路由的跳转,即用户访问一个路由,会自动跳转到另一个路由。*/}
{/* 从 /inbox/messages/:id 跳转到 /messages/:id */}
{/* :id 获取-> this.props.params.id*/}
<Redirect from="messages/:id" to="/messages/:id" />
</Route>
</Route>
   <Route path="*" component={Page404}></Route>
</Router>

app组件:

export default React.createClass({
 let baseUrl = this.props.match.url;{/*获得当前路由,即 / ,对于页面可维护性是十分重要的*/}
render() {
return (
<div>
<ul role="nav">
<IndexLink to="/" activeClassName="active">Home</IndexLink>{/*默认页*/}
<li><Link to=`/${baseUrl}/about` activeStyle={{color: 'red'}}>About</Link></li>
<li><Link to="/repos" activeClassName="active">Repos</Link></li>
         <li><Link to={{ pathname: '/hello', query: { name: 'ryan' } }}> Hello </Link></li>
         <li><Link to={`/my/${myId}/info`}>点击</Link></li>
         <li><Link to={{pathname:"/select", hash:'#ahash', query:{foo: 'bar', boo:'boz'}, state:{data:'miao'} }} activeClassName="GlobalNav-active">精选</Link></li>
</ul>
<div>
{this.props.children} {/*this.props.children属性就是子组件*/}
</div>
</div>
)
}
})

path属性:

<Route path="/hello/:name">
// 匹配 /hello/michael
// 匹配 /hello/ryan <Route path="/hello(/:name)">
// 匹配 /hello
// 匹配 /hello/michael
// 匹配 /hello/ryan <Route path="/files/*.*">
// 匹配 /files/hello.jpg
// 匹配 /files/hello.html <Route path="/files/*">
// 匹配 /files/
// 匹配 /files/a
// 匹配 /files/a/b <Route path="/**/*.jpg">
// 匹配 /files/hello.jpg
// 匹配 /files/path/to/file.jpg

path属性也可以使用相对路径(不以/开头),匹配时就会相对于父组件的路径。路由匹配规则是从上到下执行,一旦发现匹配,就不再其余的规则了。

编程式导航:

hashHistory

import { hashHistory } from 'React-router'
... ...
hashHistory.push('mine')
//传参
hashHistory.push({
pathname: 'good/details/'+this.state.id,//与路由表中配置相对应,
query: {
title:value.title,
name:value.name
},
})   
... ...

接收参数:

this.props.params.id

this.props.location.query.title
this.props.location.query.name

此外:

browserHistory

import { browserHistory } from 'react-router';
... ...
browserHistory.push('/some/path');

context对象

class Example extends React.Component {
constructor(props, context) {
super(props, context);
this.context.router; // it works
}
... ...
handleSubmit(event) {
this.context.router.push(path)
},
}
//必须显示声明
Example.contextTypes = {
router: React.PropsTypes.object
}

路由钩子:

onEnter与onLeave

onEnter钩子替代<Redirect>组件。

<Route path="inbox" component={Inbox}>
<Route
path="messages/:id"
onEnter={
({params}, replace) => replace(`/messages/${params.id}`)
}
/>
</Route>

onEnter钩子还可以用来做认证。

const requireAuth = (nextState, replace) => {
if (!auth.isAdmin()) {
// Redirect to Home page if not an Admin
replace({ pathname: '/' })
}
}
export const AdminRoutes = () => {
return (
<Route path="/admin" component={Admin} onEnter={requireAuth} />
)
}

当用户离开一个路径的时候,跳出一个提示框,要求用户确认是否离开。

const Home = withRouter(
React.createClass({
componentDidMount() {
this.props.router.setRouteLeaveHook(
this.props.route,
this.routerWillLeave
)
}, routerWillLeave(nextLocation) {
// 返回 false 会继续停留当前页面,
// 否则,返回一个字符串,会显示给用户,让其自己决定
if (!this.state.isSaved)
return '确认要离开?';
},
})
)

上面代码中,setRouteLeaveHook方法为Leave钩子指定routerWillLeave函数。该方法如果返回false,将阻止路由的切换,否则就返回一个字符串,提示用户决定是否要切换。

react 路由 react-router@3.2.1的更多相关文章

  1. react第十四单元(react路由-react路由的跳转以及路由信息)

    第十四单元(react路由-react路由的跳转以及路由信息) #课程目标 理解前端单页面应用与多页面应用的优缺点 理解react路由是前端单页面应用的核心 会使用react路由配置前端单页面应用框架 ...

  2. react第十三单元(react路由-react路由的跳转以及路由信息) #课程目标

    第十三单元(react路由-react路由的跳转以及路由信息) #课程目标 熟悉掌握路由的配置 熟悉掌握跳转路由的方式 熟悉掌握路由跳转传参的方式 可以根据对其的理解封装一个类似Vue的router- ...

  3. 【React 8/100】 React路由

    React路由 React路由介绍 现代的前端应用大多数是SPA(单页应用程序),也就是只有一个HTML页面的应用程序.因为它的用户体验更好.对服务器压力更小,所以更受欢迎.为了有效的使用单个页面来管 ...

  4. react路由配置(未完)

    React路由 React推了两个版本 一个是react-router 一个是react-router-dom 个人建议使用第二个 因为他多了一个Link组件 Npm install react-ro ...

  5. react router @4 和 vue路由 详解(七)react路由守卫

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 12.react路由守卫? a.在之前的版本中,React Router 也提供了类似的 ...

  6. 【react router路由】<Router> <Siwtch> <Route>标签

    博客 https://www.jianshu.com/p/ed5e56994f13?from=timeline 文档 http://react-guide.github.io/react-router ...

  7. react路由案例(非常适合入门)

    前面已经已经讲过一次路由   稍微有些复杂   考虑到有些同学刚接触到   我准备了一个简单的demo   就当自己也顺便复习一下 data.js const data = [ { name: 'Ta ...

  8. react路由深度解析

    先看一段代码能否秒懂很重要 这是app.js  全局js的入口 import React from 'react' import { render } from 'react-dom' import ...

  9. react路由的安装及格式和使用方法

    react路由的安装: 在要创建项目的目录命令窗里输入: cnpm install -g create-react-app create-react-app  项目名 在创建好的项目目录命令窗里输入: ...

  10. react 路由导航栏 withRouter

    codesandbox https://codesandbox.io/s/9l6prnyxjy app.js import React, { Component, Fragment } from &q ...

随机推荐

  1. QT5:第二章 布局排版控件

    一.简介 在QT组件面板中有Layouts和Spacers两个组件面板 注意:布局排版控件不显示 1.Layouts(布局) Vertical Layout:垂直方向布局,组件自动在垂直方向上分布 H ...

  2. 洛谷——P1640 [SCOI2010]连续攻击游戏

    P1640 [SCOI2010]连续攻击游戏 题目描述 lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备 ...

  3. 退役选手ZlycerQan的强大的的高精度

    #include <cstdio> #include <iostream> #include <vector> #include <iomanip> # ...

  4. 01-mysql中的数据类型

    mysql中的列数据类型:数值型.字符串类型.日期/时间类型3种 几种列类型描述使用了下述惯例:· M #表示最大显示宽度.最大有效显示宽度是255.· D #适用于浮点和定点类型,表示小数点后面的位 ...

  5. log4j 、logback 以及slf4j三者之间的关系

    在项目的开发中由于对于log4j.logback以及slf4j之间的关系和相关的知识不能清晰掌握,在业余时间进行记录. 1.三者之间的关系 1) 简答的讲就是slf4j是一系列的日志接口,而log4j ...

  6. laravel使用总结(一)

    安装 composer create-project laravel/laravel learnlaravel5 --prefer-dist v5.3.* 安装成功之后会自动生成一个key > ...

  7. Fiddler使用配置遇到的问题

    针对Fiddler使用遇到的问题记录,方便后期再使用. 1.Chrome导入证书失败,提示"提示由于存储区只读的,存储区已满..." 方法:直接去控制台添加 详细参考:http:/ ...

  8. 剑指Offer(书):删除链表的节点

    题目:在O(1)的时间内删除列表节点. /** * 步骤: * 1.检查head与removeNode节点是否为空 * 2.检查removeNode的后一个节点是否为空,不为空则使用后一个节点的值覆盖 ...

  9. 【BZOJ 3555】 [Ctsc2014]企鹅QQ(哈希)

    Description PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志.群.即时通讯.相册.集市等丰富强大 ...

  10. 南宁2017ICPC总结

    ​    ​    ​     ​    ​ 南宁2017ICPC总结 第二次到南宁,高铁三个半小时好像没什么感觉了,广西的天气真的是又湿又冷,而且交通也及其不方面,所以对广西的印象也不是很好.这次承 ...