import React from 'react';
import DataList from './data'
import Tr from './Tr'
// import One from '../One'
import User from '../User'
import Two from '../Two'
import NotFound from '../NotFound';
import {Redirect,NavLink,Route,Switch} from 'react-router-dom';
//Redirect 重定向标签 NavLink路由跳转标签 Route显示路由的窗口 Switch用来保证路由窗口只显示一个路由标签 用来包裹Route标签
class Tab extends React.Component{
render(){
return (
<div>
{
<div>
          //路由书写的规则 exact是用来精确匹配 component={组件名}
<NavLink to='/two/companies'>two</NavLink>
<NavLink to='/one/users'>one</NavLink>
<Switch>
<Redirect exact from='/' to='/one/companies' />
<Route path='/one/:type?' component={User}/>
<Route path='/two/:id?' component={Two}/>
<Route component={NotFound}></Route>
</Switch>
</div>
}</div>
)
}
} export default Tab;

子路由:

import React from 'react';
import axios from 'axios';
import OneTwo from './OneTwo'
import {Switch,NavLink,Route} from 'react-router-dom';
import NotFound from './NotFound'
class User extends React.Component{
constructor(props){
super(props);
this.state = {
list : []
}
};
componentDidMount(){ //组件挂载以后函数 通过this.props.match.params获取路由传参的值。
let {type} = this.props.match.params;
this.getData(type);
};
getData(id){
axios.get('http://localhost:4000/'+id)
.then((res)=>{
this.setState({
list:res.data
})
})
};
componentWillReceiveProps(){ //组件将更新props的值
let {type} = this.props.match.params;
this.getData(type);
}
render(){
let {list} = this.state;
return (
<div>
{list.map((item)=>{
return (
<div key={item.id}>
                  //路由字符串的写法拼接写法 对象写法在下面
<NavLink to={this.props.match.url+'/'+item.id}>{item.name}</NavLink>
</div>
)
})}
<Switch>
            //组件该显示的位置要放出循环外
<Route path="/one/users/:userid" component={OneTwo}/>
</Switch>
</div>
)
}
} export default User;

详情组件:

import React from 'react';
import axios from 'axios';
class OneTwo extends React.Component{
constructor(props){
super(props);
this.state = {
list : {}
}
}
getData(id){
    //将路由传来的id进行匹配拿到数据
axios.get('http://localhost:4000/users/'+id)
.then((res)=>{
this.setState({
list: res.data
})
})
}
componentDidMount(){
let {userid} = this.props.match.params;
this.getData(userid);
};
componentWillReceiveProps(){
let {userid} = this.props.match.params;
this.getData(userid);
}
render(){
let {list} = this.state;
return (
<div>
          //枚举对象返回一个key值数组
{Object.keys(list).map((item)=>{
return (
<div key={item}>
{item}:{list[item]}
</div>
)
})}
</div>
)
}
} export default OneTwo;

感觉就和vue一样

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

  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] 10 - Tutorial: router

    Ref: REACT JS TUTORIAL #6 - React Router & Intro to Single Page Apps with React JS Ref: REACT JS ...

  8. 七天接手react项目 系列 —— react 路由

    其他章节请看: 七天接手react项目 系列 react 路由 本篇首先讲解路由原理,接着以一个基础路由示例为起点讲述路由最基础的知识,然后讲解嵌套路由.路由传参,最后讲解路由组件和一般组件的区别,以 ...

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

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

  10. react路由深度解析

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

随机推荐

  1. angular5 组件通信(一)

    用了两年angular1,对1的组件通信比较熟练,最直接的就是直接使用scope的父子节点scope就可以实现,且基本都是基于作用域实现的通信:还有就是emit,broadcast,on这几个东西了. ...

  2. Codeforces Round #546 (Div. 2) C. Nastya Is Transposing Matrices

    C. Nastya Is Transposing Matrices time limit per test 1 second memory limit per test 256 megabytes i ...

  3. 设计模式のSingleton Pattern(单例模式)----创建模式

    单例模式没有什么好讲的,我们 举个例子 #region 单例定义 /// <summary> /// 类单例 /// </summary> private static Win ...

  4. mongodb创建用户(转发)

    参考文档: https://www.cnblogs.com/itxiongwei/p/5520863.html MongoDB 缺省是没有设置鉴权的,业界大部分使用 MongoDB 的项目也没有设置访 ...

  5. 【转】idea 2018注册码(激活码)永久性的

    百度的,上一个没用多久就挂了,这次用http://idea.toocruel.net 激活方式:License Server1.将地址 http://active.chinapyg.com/ 或者 h ...

  6. Pycharm+Python3+python工程打包成exe+在windows下自动定时运行

    python3打包成exe---pyinstaller方法:https://www.cnblogs.com/mufenglin/p/7479281.html 按照如上方式打包后,执行dist文件夹(新 ...

  7. eclipse新建maven web项目

    使用eclipse版本如下,已集成了Maven,只需要配置下即可 一.下载eclipse,解压安装 二.下载maven,解压安装 三.修改${maven_home}/config/settings.x ...

  8. 深入理解 Object.defineProperty 及实现数据双向绑定

    Object.defineProperty() 和 Proxy 对象,都可以用来对数据的劫持操作.何为数据劫持呢?就是在我们访问或者修改某个对象的某个属性的时候,通过一段代码进行拦截行为,然后进行额外 ...

  9. AJAX请求返回HTTP 400 错误 - 请求无效 (Bad request)

    在ajax请求后台数据时有时会报HTTP400错误-请求无效(Badrequest);出现这个请求无效报错说明请求没有进入到后台服务里: 原因: 1)前端提交数据的字段名称或者是字段类型和后台的实体类 ...

  10. Java IO(五)——字符流进阶及BufferedWriter、BufferedReader

    一.字符流和字节流的区别 拿一下上一篇文章的例子: package com.demo.io; import java.io.File; import java.io.FileReader; impor ...