安装

npm i react-router-dom -- save

<Router basename="/admin">
  <Route path="/" exact component={()=>{<div>首页</div>}}>
  <Route path="/about" exact component={()=>{<div>关于</div>}}>
</Router>

重定向组件

Redirect 
import React from 'react';
import './App.css';
import { BrowserRouter as Router, Route, Link, Redirect } from 'react-router-dom' function LoginInfo(props) {
console.log(props) // 这是通过 state 传参
if(props.location.state.loginState === 'success') {
return (
<Redirect to="/admin"></Redirect>
)
}else {
return (
<Redirect to="/login"></Redirect>
)
}
} let login = ()=>{
let obj = {
pathname:'/logininfo',
state:{
loginState:'success'
}
}
return (
<div>
<Link to={obj} >登录</Link>
</div>
)
}
class App extends React.Component{
render(){
return (
<Router>
<Route path="/" exact component={()=>(<div>首页</div>)}></Route>
<Route path="/login" exact component={()=>(<div> <Link to="/logininfo?loginState='success'" >登录</Link> </div>)}></Route>
<Route path="/login" exact component={login}></Route>
<Route path="/logininfo" exact component={LoginInfo}></Route>
</Router>
)
}
} export default App;

switch  内部的Route 只匹配一次

class App  extends React.Component{
render(){
return (
<Router>
<Switch>
<Route path="/" exact component={()=>(<div>首页</div>)}></Route>
<Route path="/login" exact component={()=>(<div> <Link to="/logininfo?loginState='success'" >登录</Link> </div>)}></Route>
<Route path="/login" exact component={login}></Route>
<Route path="/logininfo" exact component={LoginInfo}></Route>
<Route path="/abc" exact component={()=>(<div>首页1</div>)}></Route>
<Route path="/abc" exact component={()=>(<div>首页2</div>)}></Route>
</Switch>
</Router>
)
}
}

不使用Link  按钮直接跳转

class ChildCom extends React.Component {
render() {
return (
<button onClick={this.show}>
点击回首页
</button>
)
}
show=()=>{
// console.log(this.props);
this.props.history.push("/",{msg:'给首页的数据'}) }
} class App extends React.Component{
render(){
return (
<Router>
<Route path="/" exact component={(props)=>{console.log(props);return(<div>首页</div>)}}></Route>
</Switch>
</Router>
)
}
}

react 05 router的更多相关文章

  1. ReactJS React+Redux+Router+antDesign通用高效率开发模板,夜间模式为例

    工作比较忙,一直没有时间总结下最近学习的一些东西,为了方便前端开发,我使用React+Redux+Router+antDesign总结了一个通用的模板,这个技术栈在前端开发者中是非常常见的. 总的来说 ...

  2. Nginx支持 React browser router

    修改nginx配置文件,添加try_file配置如下,即可实现对 React browser router 的支持. location / { root /var/www/mysite; try_fi ...

  3. react 装 router - yarn add react-router-dom@next

    react 装 router yarn add react-router-dom@next

  4. [React] react+redux+router+webpack+antd环境搭建一版

    好久之前搭建的一个react执行环境,受历史影响是webpack3.10.0和webpack-dev-server2.7.1的环境,新项目准备用webpack4重新弄弄了,旧的记录就合并发布了(在没有 ...

  5. [React] 05 - Route: connect with ExpressJS

    基础: 初步理解:Node.js Express 框架 参见:[Node.js] 08 - Web Server and REST API 进阶: Ref: 如何系统地学习 Express?[该网页有 ...

  6. 用react + redux + router写一个todo

    概述 最近学习redux,打算用redux + router写了一个todo.记录下来,供以后开发时参考,相信对其他人也有用. 注意: 我只实现了Footer组件的router,其它组件的实现方法是类 ...

  7. react在router中传递数据的2种方法

    概述 不传递数据叫什么单页面应用,渲染模块还需要http请求算什么单页面应用. 本文总结了react-router4中使用BrowserRouter时传递数据的两种方法,供以后开发参考,相信对其他人也 ...

  8. [React] 10 - Tutorial: router

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

  9. React学习(3)——Router路由的使用和页面跳转

    React-Router的中文文档可以参照如下链接: http://react-guide.github.io/react-router-cn/docs/Introduction.html 文档中介绍 ...

  10. React+React Router+React-Transition-Group实现页面左右滑动+滚动位置记忆

    2018年12月17日更新: 修复在qq浏览器下执行pop跳转时页面错位问题 本文的代码已封装为npm包发布:react-slide-animation-router 在React Router中,想 ...

随机推荐

  1. 14、java.sql.SQLException: 无效的列类型: 1111 报错问题解决

    转载自 在项目中利用Mybatis做持久化框架,XXXMapper.xml中insert语句做插入时,偶然会遇到一些报错或者说是问题,如下: 一.报错日志: nested exception is o ...

  2. libcamera 简介

    libcamera 简介 libcamera 是一个开源的多媒体库,用于在 Linux 操作系统上支持多种摄像头硬件的访问,libcamera 是一个用户空间库,可用于开发基于摄像头的应用程序,如相机 ...

  3. 为 ASPNETCORE 7 项目添加 Serilog

    本文将介绍如何为 ASP.NET Core 项目添加 Serilog. 添加 Serilog 首先,我们需要在项目中添加 Serilog 的 NuGet 包. dotnet add package S ...

  4. FPGA:乒乓球比赛模拟机的设计

    简介 开发板:EGO1 开发环境:Windows10 + Xilinx Vivado 2020 数字逻辑大作业题目 7: 乒乓球比赛模拟机的设计 乒乓球比赛模拟机用发光二极管(LED)模拟乒乓球运动轨 ...

  5. [C++]模版特例化和模版偏特化

    函数模版特例化 例子: //第一个版本;可以比较任意两个类型 template<typename T> int compare(const &T,const T&); // ...

  6. 简述HashSet的扩容机制以及我们在重写equals()的时候为何会重写hashcode()

    简述HashSet的扩容机制以及我们在重写equals()的时候为何会重写hashcode()   摘要:在背面试知识点的时候存在这样一条著名的面试题:我们重写equals()的时候为什么要重写has ...

  7. P8796 [蓝桥杯 2022 国 AC] 替换字符

    题面 给定一个仅含小写英文字母的字符串 \(s\) 和 \(m\) 次操作,每次操作选择一个区间 \([l_i,r_i]\) 将 \(s\) 的该区间中的所有字母 \(x_i\) 全部替换成字母 \( ...

  8. .NetCore下基于FreeRedis实现的Redis6.0客户端缓存之缓存键条件优雅过滤

    前言 众所周知内存缓存(MemoryCache)数据是从内存中获取,性能表现上是最优的,但是内存缓存有一个缺点就是不支持分布式,数据在各个部署节点上各存一份,每份缓存的过期时间不一致,会导致幻读等各种 ...

  9. 【Java刷题】初始化List应该选择ArrayList还是LinkedList

    文章目录 前言 题目 解题思路 遇到的坑:ArrayList和LinkedList 未通过代码 通过代码 小结 ArrayList和LinkedList的区别 在刷题的时候应该如何选择 排序效率 测试 ...

  10. uniapp如何打包wgt格式

    打包 build 首次打包,需要配置AppId 登录dcloud开发者中心 点击直达 创建应用 将生成的AppId配置到项目manifest.json中 开始打包 打包成功