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. DirectX9:高级着色语言(HLSL)

    一.简介 高级着色语言(High)可以编写顶点着色器和像素着色器,取代固定功能流水线中的部分功能,在图形卡的GPU(Graphics Processing Unit,图形处理单元)中执行 注意:如果图 ...

  2. windwos .bat脚本大全

    记录一个很有用比较全面的windows .bat脚本网站 https://www.cnblogs.com/zhaoqingqing/p/4620402.html

  3. 漫谈Word2vec之skip-gram模型

    https://zhuanlan.zhihu.com/p/30302498 陈运文 ​ 复旦大学 计算机应用技术博士 40 人赞同了该文章 [作者] 刘书龙,现任达观数据技术部工程师,兴趣方向主要为自 ...

  4. CentOS7.4搭建kafka单结点和集群

    操作系统选择 CentOS7.4x86-64(操作系统的x86_64是跟CPU有关的,最早AMD公司开发出了一款向下兼容x86CPU,向上又扩充了指令集,具有了64位CPU的特性,这款CPU后来改名为 ...

  5. C/C++连接MySQL数据库执行查询

    1. 简介: 使用C/C++连接MySQL数据库执行增删改查操作,基本就是围绕以下两个文件展开: mysql.h(此头文件一般在MySQL的include文件夹内,如 D:\MySQL\mysql-5 ...

  6. linux 05

    日期与时间命令:date.日历的命令:cal.计算器:bc  要使用quit退出 在命令行模式里执行命令时,会有两种主要情况: 一种是该命令会直接显示结果然后回到命令提示符等待下一个命令的输入 dat ...

  7. Tarjan 算法求割点、 割边、 强联通分量

    Tarjan算法是一个基于dfs的搜索算法, 可以在O(N+M)的复杂度内求出图的割点.割边和强联通分量等信息. https://www.cnblogs.com/shadowland/p/587225 ...

  8. linux中.bashrc 等文件中的rc是什么意思

    英文原义:RC (run command)中文释义:含有程序(应用程序甚至操作系统)启动指令的脚本文件 注 解:这一文件在操作系统启动时会自动执行,它含有要运行的指令(命令或其它脚本)列表. 相当于w ...

  9. python基础知识--条件判断和循环

    一.输入输出 python怎么来接收用户输入呢,使用input函数,python2中使用raw_input,接收的是一个字符串,输出呢,第一个程序已经写的使用print,代码入下: 1 name=in ...

  10. BZOJ1704: [Usaco2007 Mar]Face The Right Way 自动转身机

    n<=5000个数0或1,每次可以连续对固定长度区间取反,目标把所有1变0,求一个取反区间的固定长度K使取反次数最少. 答案关于K不单调,因此枚举K,对每个K扫一遍区间,遇到1就把连续K个数反转 ...