1、新增知识

/*
实现js跳转路由:https://reacttraining.com/react-router/web/example/auth-workflow
1、要引入Redirect
import { BrowserRouter as Router,Route,Link,Redirect, withRouter
} from "react-router-dom"; 2、定义一个flag
this.state = {
loginFlag:false
}; 3、render里面判断flag 来决定是否跳转
if(this.state.loginFlag){
return <Redirect to={{ pathname: "/" }} />;
} 4、要执行js跳转
通过js改变loginFlag的状态
改变以后从新render 就可以通过Redirect自己来跳转
*/

2、代码实现案例

import React, { Component } from 'react';
import {Redirect} from "react-router-dom";
class Login extends Component {
constructor(props) {
super(props);
this.state = {
loginFlag:false
};
} doLogin=(e)=>{
e.preventDefault();//防止跳转
let username=this.refs.username.value;
let password=this.refs.password.value;
console.log(username,password)
if(username=='admin' && password==''){
//登录成功 跳转到首页
this.setState({
loginFlag:true
})
}else{
alert('登录失败')
}
} render() {
if(this.state.loginFlag){
// return <Redirect to={{ pathname: "/" }} />;
return <Redirect to='/' />;
}
return (
<div>
<br /> <br /> <br />
<form onSubmit={this.doLogin}>
<input type="text" ref="username" /> <br /> <br />
<input type="password" ref="password" /> <br /> <br />
<input type="submit" value="执行登录"/>
</form>
</div>
);
}
}
export default Login;

3、嵌套路由知识点

       return <Route  key={key}  path={route.path}
render={props => (
// pass the sub-routes down to keep nesting
<route.component {...props} routes={route.routes} />
)}
/>

4、嵌套路由案例,App.js

import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
import './assets/css/index.css';
import routes from './model/router.js';
class App extends Component {
render() {
return (
<Router>
<div>
<header className="title">
<Link to="/">首页组件</Link>
<Link to="/user">用户页面</Link>
<Link to="/shop">商户</Link>
<Link to="/news">新闻</Link>
</header>
{
routes.map((route,key)=>{
if(route.exact){
return <Route key={key} exact path={route.path}
// route.component value.component <User {...props} routes={route.routes} />
render={props => (
// pass the sub-routes down to keep nesting
<route.component {...props} routes={route.routes} />
)}
/>
}else{
return <Route key={key} path={route.path}
render={props => (
// pass the sub-routes down to keep nesting
<route.component {...props} routes={route.routes} />
)}
/>
}
})
}
</div>
</Router>
);
}
} export default App;

5、router.js文件

let routes = [
{
path: "/",
component: Home,
exact:true
},
{
path: "/shop",
component: Shop
},
{
path: "/user",
component: User,
routes:[ /*嵌套路由*/
{
path: "/user/",
component: UserList
},
{
path: "/user/add",
component: UserAdd
},
{
path: "/user/edit",
component: UserEdit
}
]
},
{
path: "/news",
component: News
}
]; export default routes;

Reactjs之实现js跳转路由的更多相关文章

  1. 十六、React 渲染数据注意事项、以及react-router4.x中使用js跳转路由(登录成功自动跳转首页)

    一.React加载数据流程回顾 先看上一节的产品详情代码:https://blog.csdn.net/u010132177/article/details/103184176 [Pcontent.js ...

  2. 4.JS跳转路由/刷新/返回页面

    1.JS跳转路由(需要拿到父组件的history) clickHandle(){ let history = this.props.history; history.push( '/home') } ...

  3. React之js实现跳转路由

    1.新增知识 /* 实现js跳转路由:https://reacttraining.com/react-router/web/example/auth-workflow 1.要引入Redirect im ...

  4. Vue 编程式导航(通过js跳转页面)以及路由hash模式和history模式

    第一种方法: this.$router.push({path:'shopcontent?aid=3'}   第二种方法   this.$router.push({name:'news'}} 通过在ma ...

  5. 试着用React写项目-利用react-router解决跳转路由等问题(三)

    转载请注明出处:王亟亟的大牛之路 本来想一下子把路由的接下来的内容都写完的,但是今天白天开了会,传了些代码打了个包,就被耽搁了 这一篇来讲一下 IndexLink和 onlyActiveOnIndex ...

  6. 使用Js控制ReactRouter路由

    [使用Js控制ReactRouter路由] 首先引入PropTypes: const PropTypes = require('prop-types'); 然后定义context的router属性: ...

  7. vue.js编程式路由导航 --- 由浅入深

    编程式路由导航 实例中定义一个方法,这个方法绑定在标签上 然后就设置路由跳转 语法 this.$router.history.push('要跳转路由的地址') <!DOCTYPE html> ...

  8. react-router5.x 的配置及其页面跳转方法和js跳转方法

    https://blog.csdn.net/sinat_37255207/article/details/90745207 上次用react-router 的时候  还是3.x 很久不用 已经到rea ...

  9. vue使用vue-router beforEach实现判断用户登录跳转路由筛选

    vue使用vue-router beforEach实现判断用户登录跳转路由筛选 :https://www.colabug.com/3306814.html 在开发webApp的时候,考虑到用户体验,经 ...

随机推荐

  1. Linux 查看主机、CPU、内存、内核、网卡或MAC地址、关机、重启、当前使用人、网络连接状态、主机目前使用状态

    7 uname -a 显示主机名.内核.硬件结构等全部信息 unmae -r 只显示内核 查看Redhat和centos的内核版本也可以用cat /etc/redhat-release 或cat /e ...

  2. new和delete用法小结

    在C语言中是利用库函数 malloc 和 free 函数来分配和撤销内存的.C++提供了较简便而功能较强的运算符 new 和 delete 来取代 malloc 和 free 函数. new 和 de ...

  3. 多项式FFT/NTT模板(含乘法/逆元/log/exp/求导/积分/快速幂)

    自己整理出来的模板 存在的问题: 1.多项式求逆常数过大(尤其是浮点数FFT) 2.log只支持f[0]=1的情况,exp只支持f[0]=0的情况 有待进一步修改和完善 FFT: #include&l ...

  4. ${filename}用法一:${file内部的#%的匹配方式}

    假设我们定义了一个变量为: file=/dir1/dir2/dir3/my.file.txt 我们可以用${ }分别替换获得不同的值: ${file#*/}:拿掉第一条/及其左边的字串:dir1/di ...

  5. 阅读之Java多线程

    Java多线程 用多线程只有一个目的,就是更好的利用cpu的资源,因为所有的多线程代码都可以用单线程来实现. 多线程:指的是这个程序(一个进程)运行时产生了不止一个线程 并行与并发: 并行:多个cpu ...

  6. jquery visible 选择器 语法

    jquery visible 选择器 语法 作用::visible 选择器选取每个当前是可见的元素.除以下几种情况之外的元素即是可见元素:设置为 display:none   type="h ...

  7. 51 Nod 最大子矩阵和

    1051 最大子矩阵和  基准时间限制:2 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 一个M*N的矩阵,找到此矩阵的一个子矩阵,并且这个子矩阵的元素的和是最大的 ...

  8. linux下简单好用的端口映射转发工具rinetd

    linux下简单好用的工具rinetd,实现端口映射/转发/重定向官网地址http://www.boutell.com/rinetd 软件下载wget http://www.boutell.com/r ...

  9. UE4 使用VaRest的最佳实践

    背景介绍: 用Node.js,express,Mongo搭建了一个简单后台,为项目提供REST风格的API服务. 第一个查询是通过Get进行,返回一个json字符串. 在虚幻里使用VaRest来进行访 ...

  10. koa2,koa-jwt中token验证实战详解

    用户身份验证通常有两种方式,一种是基于cookie的认证方式,另一种是基于token的认证方式.当前常见的无疑是基于token的认证方式.以下所提到的koa均为koa2版本. token认证的优点是无 ...