美文网首页
React 保护式路由

React 保护式路由

作者: Fortune_Cookie | 来源:发表于2018-12-28 13:42 被阅读0次

大多数情况中,我们必须先验证登录状态才能进入到主页,所以需要保护式路由。这里,需要保护的路由是Admin,如果登录没通过则先进入Login路由组件。

class App5 extends React.Component {
    render(){
        return (
            <div className="app5">
                <ul>
                    <li>
                        <Link to='/'>Home</Link>
                    </li>
                    <li>
                        <Link to='/category'>Category</Link>
                    </li>
                    <li>
                        <Link to='/products'>Products</Link>
                    </li>
                    <li>
                        <Link to='/admin'>Admin</Link>
                    </li>
                </ul>
                <Route exact path='/' component={Home} />
                <Route path='/category' component={Category} />
                <Route path='/products' component={Products} />
                <Route path='/login' component={Login} />

                {/*自定义路由*/}
                <PrivateRoute path='/admin' component={Admin} />
            </div>
        )
    }
}

const Home = props => <h2>This is Home {console.log('Home-Props')}{console.log(props)}</h2>

const Admin = () => <h2>Welcome to admin!</h2>


// 自定义路由
const PrivateRoute = (({component:Component,...rest}) => {
    return (
        <Route
            {...rest}
            render={props =>
                // 如果登录验证通过则进入Admin路由组件
                fakeAuth.isAuthenticated === true
                ?(<Component />)
                // 将from设置为Admin路由pathname,并传递给子组件Login
                :(<Redirect to={{pathname:'/login',state:{from:props.location.pathname}}} />)
            }
         />
    )
})



export default App5

Login路由组件如下

class Login extends React.Component {
    constructor(){
        super()
        this.state = {
            toAdmin:false
        }
    }

    login = () =>{
        fakeAuth.authenticate(() => {
            this.setState({
                toAdmin:true
            })
        })
    }

    render(){
        const from = this.props.location.state.from
        const toAdmin = this.state.toAdmin
        if(toAdmin) {
            return (
                <Redirect to={from} />
            )
        }
        return (
            <div className="login">
            {console.log(this.props)}
                <p>You must log in then go to the{from} </p>
                <button onClick={this.login}>
                    Log in
                </button>
            </div>
        )
    }
}

export default Login


export const fakeAuth = {
    // 验证状态
    isAuthenticated:false,
    authenticate(cb){
        this.isAuthenticated = true
        setTimeout(cb,100)
    }
}

打印台中 从Admin路由组件传递给子组件LoginProps如图

Props
// 自定义路由
const PrivateRoute = (({component:Component,...rest}) => {
    return (
        <Route
            {...rest}
            render={props =>
                // 如果登录验证通过则进入Admin路由组件
                fakeAuth.isAuthenticated === true
                ?(<Component />)
                // 将from设置为Admin路由pathname,并传递给子组件Login
                :(<Redirect to={{pathname:'/login',state:{from:props.location.pathname}}} />)
            }
         />
    )
})

自定义路由传递给Login组件state也在里面

相关文章

  • React 保护式路由

    大多数情况中,我们必须先验证登录状态才能进入到主页,所以需要保护式路由。这里,需要保护的路由是Admin,如果登录...

  • ⻚⾯连接器之路由react-router

    安装 特点react⼀切皆组件,路由也是组件分布式的配置, 分布在你页面的每个角落包含式配置, 可匹配多个路由 写...

  • react从0到1的探索记录04

    18、React中的路由 React中路由的基本使用 在react中使用路由首先需要安装react的路由模块 np...

  • React-Router v4 学习

    React-Router v4 1. 设计理念1.1. 动态路由1.2. 嵌套路由1.3. 响应式路由 2. 快速...

  • react学习资料八

    路由 使用react的路由就要引入react路由插件react-router.js ReactRouter 保存一...

  • React-Router知识点

    路由的分类 页面路由 hash 路由 h5路由 react路由 react-router-dom 路由方式 h5路...

  • React路由

    React 路由 一、在React中使用react-router-dom路由 安装npm install reac...

  • 有标题的文章

    React Redux 路由设计 - - React

  • 怎么理解react和虚拟DOM

    1、怎么理解react react只关注视图层,不关注路由和数据。 它引入虚拟DOM的概念,利用函数式编程的思想。...

  • React路由

    React路由 一、路由的形式 hash路由 : HashRouter history路由 : BrowserRo...

网友评论

      本文标题:React 保护式路由

      本文链接:https://www.haomeiwen.com/subject/qphslqtx.html