which should render to jump from one use return <Redirect to = "/" /> without this.props.history.replace ( '/'); because there must render return
this.props.history.replace by clicking inside the callback event
1.admin.js inside
< Script > the render () { // read the saved user, if there is no direct jump to the login page const User = the JSON.parse (localStorage.getItem ( ' user_key ' ) || ' {} ' ) IF (! User ._id) { // this.props.history.replace ( '/ Login') // event callback using return < the Redirect to = " / Login " / > } </ Script >
2.login.js inside
< Script > IF (res.code === ' 0000 ' ) { // jump to the page management const User = res.data localStorage.setItem ( ' user_key ' , the JSON.stringify (User))
//storageUtils.saveUser(user) improved version
the this .props.history.replace ( ' / ' ) message.success ( ' successful login ' ) } the else { Message.Error An (res.msg) // res.msg information returned by the backend } the render () { const User = the JSON.parse (localStorage.getItem ( ' user_key ' ) || ' {} ' )
// improved version: const user = storageUtils.getUser ()
if (user._id) { return <Redirect to="/" /> } </script>
Package to save the user reads the user, as a method to delete a user
<script> const USER_KEY = 'user_key' export default { //返回user对象,如果没有返回{} getUser() { return JSON.parse(localStorage.getItem(USER_KEY)||'{}') }, //保存user saveUser(user) { localStorage.setItem(USER_KEY,JSON.stringify(user)) }, //删除user removeUser() { localStorage.removeItem(USER_KEY) } } </script>