react 路由 react-router-dom
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的更多相关文章
- react第十四单元(react路由-react路由的跳转以及路由信息)
第十四单元(react路由-react路由的跳转以及路由信息) #课程目标 理解前端单页面应用与多页面应用的优缺点 理解react路由是前端单页面应用的核心 会使用react路由配置前端单页面应用框架 ...
- react第十三单元(react路由-react路由的跳转以及路由信息) #课程目标
第十三单元(react路由-react路由的跳转以及路由信息) #课程目标 熟悉掌握路由的配置 熟悉掌握跳转路由的方式 熟悉掌握路由跳转传参的方式 可以根据对其的理解封装一个类似Vue的router- ...
- 【React 8/100】 React路由
React路由 React路由介绍 现代的前端应用大多数是SPA(单页应用程序),也就是只有一个HTML页面的应用程序.因为它的用户体验更好.对服务器压力更小,所以更受欢迎.为了有效的使用单个页面来管 ...
- react路由配置(未完)
React路由 React推了两个版本 一个是react-router 一个是react-router-dom 个人建议使用第二个 因为他多了一个Link组件 Npm install react-ro ...
- react router @4 和 vue路由 详解(七)react路由守卫
完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 12.react路由守卫? a.在之前的版本中,React Router 也提供了类似的 ...
- 【react router路由】<Router> <Siwtch> <Route>标签
博客 https://www.jianshu.com/p/ed5e56994f13?from=timeline 文档 http://react-guide.github.io/react-router ...
- [React] 10 - Tutorial: router
Ref: REACT JS TUTORIAL #6 - React Router & Intro to Single Page Apps with React JS Ref: REACT JS ...
- 七天接手react项目 系列 —— react 路由
其他章节请看: 七天接手react项目 系列 react 路由 本篇首先讲解路由原理,接着以一个基础路由示例为起点讲述路由最基础的知识,然后讲解嵌套路由.路由传参,最后讲解路由组件和一般组件的区别,以 ...
- react路由案例(非常适合入门)
前面已经已经讲过一次路由 稍微有些复杂 考虑到有些同学刚接触到 我准备了一个简单的demo 就当自己也顺便复习一下 data.js const data = [ { name: 'Ta ...
- react路由深度解析
先看一段代码能否秒懂很重要 这是app.js 全局js的入口 import React from 'react' import { render } from 'react-dom' import ...
随机推荐
- SQlite源码分析-体系结构
体系结构 在内部,SQLite由以下几个组件组成:内核.SQL编译器.后端以及附件.SQLite通过利用虚拟机和虚拟数据库引擎(VDBE),使调试.修改和扩展SQLite的内核变得更加方便.所有SQL ...
- elasticSearch学习安装
资料: 1.Elasticsearch学习,请先看这一篇! https://blog.csdn.net/laoyang360/article/details/52244917 2. linux下ela ...
- Mysql SQL分组取每组前几条记录
按name分组取最大的两个val: [比当前记录val大的条数]小于2条:即当前记录为为分组中的前两条 > (select count(*) from tb where name = a.nam ...
- 简单的C#TCP协议收发数据示例
参考:http://www.cnblogs.com/jzxx/p/5630516.html 一.原作者的这段话很好,先引用一下: Socket的Send方法,并非大家想象中的从一个端口发送消息到另一个 ...
- CMD命令行netsh添加防火墙规则
1.为e:\f.exe 添加防火墙规则(参考:https://www.cnblogs.com/zhen656/p/4275270.html),需要管理员权限. >netsh advfirewal ...
- UVA1434-The Rotation Game(迭代加深搜索)
Problem UVA1434-The Rotation Game Accept:2209 Submit:203 Time Limit: 3000 mSec Problem Description ...
- @ModelAttribute
在执行Controller方法前都会新建一个Map对象称为隐含模型,该Map对象是共享的,如果一个方法的入参为Map ModelAndMap ModelMap等类型,那么会把隐含模型当做入参赋给方法. ...
- 002_cookie的session_id解释
HTTP协议(http://www.w3.org/Protocols/)是“一次性单向”协议. 服务端不能主动连接客户端,只能被动等待并答复客户端请求.客户端连接服务端,发出一个HTTP Reques ...
- CentOS 软件安装(yum 和 rpm)
CentOS 软件安装方法 常用的分为两种, - yum install 安装包名 : 类似于 Debian 的 “ apt-get install 安装包名 “ - rpm -i rmp文件名 :类 ...
- Python实现机器人聊天
今天午休的时候,无意之中看了一篇博客,名字叫Python实现机器人,感觉挺有的意思的.于是用其写了一个简单的Python聊天,源码如下所示: # -*- coding: utf- -*- import ...