React实现js跳转路由
/* 实现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自己来跳转 */ 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=='123456'){ //登录成功 跳转到首页 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;
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!
posted on 2020-03-16 19:15 LoaderMan 阅读(1480) 评论(0) 编辑 收藏 举报