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 ...
随机推荐
- WPF模板(二)应用
本次内容来源于电子书,和上一篇一样. 在WPF中有三大模板ControlTemplate,ItemsPanelTemplate,DataTemplate.其中ControlTemplate和Items ...
- 15.selenium_case01
# 与百度首页交互 from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait f ...
- https的证书认证 iOS版
一.证书链 SecTrustRef: SecTrustRef trust = challenge.protectionSpace.serverTrust; 需要先拿出一个 SecTrustRef 对象 ...
- linux 基本操作
终端常用快捷键 ctrl+c 中断运行ctrl+d 退出中断ctrl+s 暂停当前程序,暂停后按下任意键恢复运行 ctrl+k 删除从光标所在位置到行末 Alt+Backspace 向前删除一个单词 ...
- 获取列表的索引操作:enumerate
通过循环获取列表的索引操作: 主要使用:enumerate product_list = [['Iphone7',5800], ['Coffee',30], ['疙瘩汤',10], ['Python ...
- 【转】CefSharp语言(Locales)本地化问题
CefSharp默认是嵌入的chrome浏览器内核,默认英文,所以右键菜单以及一些功能都是英文显示. 国内用需要汉化.CefSharp本身支持本地化Locales,需要在初始化Browser的时候设置 ...
- Kali-linux使用Metasploit基础
Metasploit是一款开源的安全漏洞检测工具.它可以帮助用户识别安全问题,验证漏洞的缓解措施,并对某些软件进行安全性评估,提供真正的安全风险情报.当用户第一次接触Metasploit渗透测试框架软 ...
- linux命令之df
功能:查看文件系统的磁盘空间使用情况 常用选项: -a 包含虚拟文件系统 -h 可易读单位显示 -i 显示 inode 信息而非块使用量 -k 1K 块的数量 -t 只显示指定文件系统为指定类型的信息 ...
- java操作Redis缓存设置过期时间
关于Redis的概念和应用本文就不再详解了,说一下怎么在java应用中设置过期时间. 在应用中我们会需要使用redis设置过期时间,比如单点登录中我们需要随机生成一个token作为key,将用户的信息 ...
- 最长上升子序列(LIS)
(我先扯些没用的) 我这个笨孩子 学点东西好慢好慢的 我还贪玩 于是 将自己陷入了一个超级超级超级差的境地 可 我还傻乎乎的保有着天真的梦想(理想?) 所以现在我要加倍的努力努力再努力了 只能嘎油了 ...