React-router 要点
1.关于url中传参的问题
比如我想打开:
/articles/detail/101
在url中要传一个参数
/articles/detail/:articleId
路由中:
<Route path="/articles/detail/:articleId" component={ArticleDetail}/> Link中:
<Link to="/article/detail/101">文章</Link> 如何在页面中取到参数
<div>{this.props.params.articleId}</div>
2.如何用js来实现页面路由的跳转
在react-router中,两种方法:
第一种,使用withRouter(),然后将在内部可以获取this.props.router
第二种,使用this.context.router.push('/'),不过在使用前必须定义这个类的contextTypes
withRouter怎么用?
import React from 'react'
import {withRouter} from 'react-router' class App extends React.Component {
...
this.props.router.push('/')
} export default withRouter(App)
用context怎么用呢?
...
export default class App extends React.Component {
...
this.context.router.push('/')
}
App.contextTypes = {
router: React.PropsTypes.object
} //等同于下面的写法 ...
export default React.createClass({
contextTypes: {
router: React.PropsTypes.object
}
...
this.context.router.push('/')
...
})
转自 http://www.jianshu.com/p/0e54c6b6ab2b 作者 JasonFF
React-router 要点的更多相关文章
- [Redux] Filtering Redux State with React Router Params
We will learn how adding React Router shifts the balance of responsibilities, and how the components ...
- [转] React Router 使用教程
PS:react-route就是一个决定生成什么父子关系的组件,一般和layout结合起来,保证layout不行,内部的子html进行跳转 你会发现,它不是一个库,也不是一个框架,而是一个庞大的体系. ...
- [Redux] Navigating with React Router <Link>
We will learn how to change the address bar using a component from React Router. In Root.js: We need ...
- [Redux] Adding React Router to the Project
We will learn how to add React Router to a Redux project and make it render our root component. Inst ...
- React Router基础使用
React是个技术栈,单单使用React很难构建复杂的Web应用程序,很多情况下我们需要引入其他相关的技术 React Router是React的路由库,保持相关页面部件与URL间的同步 下面就来简单 ...
- 最新的chart 聊天功能( webpack2 + react + router + redux + scss + nodejs + express + mysql + es6/7)
请表明转载链接: 我是一个喜欢捣腾的人,没事总喜欢学点新东西,可能现在用不到,但是不保证下一刻用不到. 我一直从事的是依赖angular.js 的web开发,但是我怎么能一直用它呢?看看最近火的一塌糊 ...
- react router 4.0以上的路由应用
thead>tr>th{padding:8px;line-height:1.4285714;border-top:1px solid #ddd}.table>thead>tr& ...
- React Router 使用教程
一.基本用法 React Router 安装命令如下. $ npm install -S react-router 使用时,路由器Router就是React的一个组件. import { Router ...
- 关于react router 4 的小实践
详细代码栗子:https://github.com/wayaha/react-dom-CY clone然后 npm install npm start 分割线 1.这个项目使用create-react ...
- React Router 4.x 开发,这些雷区我们都帮你踩过了
前言 在前端框架层出不穷的今天,React 以其虚拟 DOM .组件化开发思想等特性迅速占据了主流位置,成为前端开发工程师热衷的 Javascript 库.作为 React 体系中的重要组成部分:Re ...
随机推荐
- centos x86_64--------------------------------系统调用
http://blog.csdn.net/hmsiwtv/article/details/11022241 [root@monitor ~]# cat /usr/include/asm/unistd. ...
- 结构体定义 typedef struct 用法详解和用法小结
typedef是类型定义的意思.typedef struct 是为了使用这个结构体方便.具体区别在于:若struct node {}这样来定义结构体的话.在申请node 的变量时,需要这样写,stru ...
- 115 Java Interview Questions and Answers – The ULTIMATE List--reference
In this tutorial we will discuss about different types of questions that can be used in a Java inter ...
- hive-安装MySQL(centos6.4)
为安装hive做准备,以前装过无数次,在线的.tar包的,一直不用忘得差不多了. centos6.4 虚拟机 先看有没有装,有的话应该是自带的,卸载就可以了 命令分别是 然后在线安装,命令是 (-y是 ...
- Javascript 数组与字典
Javascript 的数组Array,既是一个数组,也是一个字典(Dictionary). 先举例看看数组的用法. var a = new Array(); a[0] = "Acer&qu ...
- 从wordcount 开始 mapreduce (C++\hadoop streaming模式)
序:终于开始接触hadoop了,从wordcount开始 1. 采用hadoop streamming模式 优点:支持C++ pathon shell 等多种语言,学习成本较低,不需要了解hadoop ...
- html笔记03:表单
1.表单是用来收集用户填写的信息,可以说表单就是一个容器,里面的元素的类型可以不一样,所表示的功能也不同. 表单基本语法: <html> <head> <title> ...
- 前端必会css整理
1.设置css样式的三种方式? 外部样式表,引入一个外部css文件 内部样式表,将css代码放在<head>标签内部 内联样式,将css样式 ...
- ASP.NET弹出模态对话框【转】
主页面 PageBase.aspx.cs 中的代码 protected void Page_Load(object sender, EventArgs e) { if (!this.IsPost ...
- android 读取串口数据的服务
2016-09-1813:10:03 继承Service,定义抽象方法onDataReceived,子类通过实现抽象方法获取接收到数据的回调. package com.zrsoft.liftad.se ...