react的路由权限控制
<p>在使用路由的时候,有的时候我们的界面只能够在登录之后才可以看的到,这个时候就需要使用路由权限控制了</p><p>找了资料发现一个就是我使用的方法,一个是高阶组件。 原谅菜鸟看不太懂不会使用高阶组件…………</p>
<p>首先在路由中做一个私有权限的限制,限制里面的path就是你的私有界面</p>
<p> router.js</p>
<div class="cnblogs_code">
<pre> <Switch>
<Route path="/" exact component={Home} />
<PrivateRoutepath="/MyOptional"component={MyOptional} />
<Route render={() => <Redirect to="/" />} />
</Switch></pre>
</div>
<p>想要跳转到path的里面,首先在PrivateRoute里面做登录判断条件</p>
<p>private.js</p>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import {Route,Redirect,withRouter} from </span>'react-router-dom'<span style="color: rgba(0, 0, 0, 1)">;
import PropTypes from </span>'prop-types'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">私有路由,只有登录的用户才能访问</span>
<span style="color: rgba(0, 0, 0, 1)">class PrivateRoute extends React.Component{
componentWillMount(){
letisAuthenticated </span>=sessionStorage.getItem("userName") ? <span style="color: rgba(0, 0, 255, 1)">true</span> :<span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setState({isAuthenticated:isAuthenticated})
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(!<span style="color: rgba(0, 0, 0, 1)">isAuthenticated){
const {history} </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props;
setTimeout(() </span>=><span style="color: rgba(0, 0, 0, 1)"> {
history.replace(</span>"/"<span style="color: rgba(0, 0, 0, 1)">);
}, </span>1000<span style="color: rgba(0, 0, 0, 1)">)
}
}
render(){
let { component: Component,path</span>="/",exact=<span style="color: rgba(0, 0, 255, 1)">false</span>,strict=<span style="color: rgba(0, 0, 255, 1)">false</span>} = <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props;
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">this</span>.state.isAuthenticated ?<span style="color: rgba(0, 0, 0, 1)">(
</span><Routepath={path} exact={exact}strict={strict}render={(props)=>( <Component {...props} /> )} /><span style="color: rgba(0, 0, 0, 1)">
) :</span><<span style="color: rgba(0, 0, 0, 1)">Redirect
to</span>=<span style="color: rgba(0, 0, 0, 1)">{{
pathname: </span>"/"<span style="color: rgba(0, 0, 0, 1)">,
}} //这里必须使用redireact不能和上面一样使用<Route/>因为会出现页面先跳转到myOption然后再跳转到首页的状况,这样用户体验不好
</span>/> ;
<span style="color: rgba(0, 0, 0, 1)"> }
}
PrivateRoute.propTypes</span>=<span style="color: rgba(0, 0, 0, 1)">{
path:PropTypes.string.isRequired,
exact:PropTypes.bool,
strict:PropTypes.bool,
component:PropTypes.func.isRequired
}
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> withRouter(PrivateRoute);</pre>
</div>
<p>这样就ok了</p>
<p>注:因为我的登录界面是在首页或者各个界面的模态框,所以这里我的路径直接都是如果没有登录,直接跳转首页的。如果大家的登录界面是单独的,那可以直接跳转到登录界面了</p>
<p>还有个tips就是,如果你的界面没有在路由中进行声明,然后又想要在界面中使用route相关的路径参数,就可以引入withRouter,在this.props中得到了</p><br><br>
来源:https://www.cnblogs.com/yesu/p/10973526.html
頁:
[1]