Reactjs react路由器可以';t重定向到受保护的路由(仅更改URL)
在我的react应用程序中,我试图在than登录后重定向到仪表板,但由于than受保护的路由名称不是“路由”,react路由器无法将其检测为路由,只需更改URL即可。 我已经检查过无保护和简单的路由,比如“注册”和像重定向过程中的符咒一样加载 路由器4似乎无法将受保护的路由检测为路由 以下是我在app.jsx中的路线:Reactjs react路由器可以';t重定向到受保护的路由(仅更改URL),reactjs,react-router,router,react-router-v4,react-router-dom,Reactjs,React Router,Router,React Router V4,React Router Dom,在我的react应用程序中,我试图在than登录后重定向到仪表板,但由于than受保护的路由名称不是“路由”,react路由器无法将其检测为路由,只需更改URL即可。 我已经检查过无保护和简单的路由,比如“注册”和像重定向过程中的符咒一样加载 路由器4似乎无法将受保护的路由检测为路由 以下是我在app.jsx中的路线: const routes = ( <Router> <div> <Route exact path=
const routes = (
<Router>
<div>
<Route exact path="/" component={Login}/>
<Route path="/login" component={Login}/>
<Route path="/register" component={Register}/>
<Route path="/resetpassword" component={ResetPassword}/>
<RestaurantAuthRoute path="/dashboard" component={RPDashboard}/>
<RestaurantAuthRoute path="/dashboard/addfood" component={RPAddFood}/>
</div>
</Router>
);
const路由=(
);
RestaurantAuthRoute.jsx:
var {connect} = require('react-redux');
var loginAuth = require('../api/loginAuth');
import {bindActionCreators} from 'redux';
import * as actions from 'actions';
import { Route, Redirect } from 'react-router-dom';
class RestaurantAuthRoute extends React.Component {
constructor(props){
super(props);
}
componentWillMount() {
var mytoken = localStorage.getItem('token');
var that = this;
loginAuth.restaurantAuthCheck(mytoken).then( function(res){
if(res.success == true){
that.props.actions.setRestaurantAuth(true);
}
else if(res.success == false){
that.props.actions.setRestaurantAuth(false);
}
});
}
render() {
if(this.props.restaurantLog.status == true || this.props.restaurantLog.status == false){
const { component: Component, restaurantLog, ...rest } = this.props;
return (
<Route {...rest} render={props => {
return restaurantLog.status
? <Component {...props} />
: <Redirect to="/login" />
}} />
)
}
else{
return(<p>wait</p>)
}
}
}
function mapStateToProps(state, ownProps){
return{
restaurantLog: state.logRestaurantState
}
}
function MapDispatchToProps(dispatch){
return{
actions: bindActionCreators(actions, dispatch)
}
}
export default connect(
mapStateToProps,
MapDispatchToProps
)(RestaurantAuthRoute);
var{connect}=require('react-redux');
var loginAuth=require('../api/loginAuth');
从“redux”导入{bindActionCreators};
从“操作”导入*作为操作;
从“react router dom”导入{Route,Redirect};
类RestaurantAuthRoute扩展React.Component{
建造师(道具){
超级(道具);
}
组件willmount(){
var mytoken=localStorage.getItem('token');
var=这个;
loginAuth.restaurantuthcheck(mytoken)。然后(函数(res){
如果(res.success==true){
那。道具。动作。setRestaurantAuth(真);
}
else if(res.success==false){
即.props.actions.setRestaurantAuth(false);
}
});
}
render(){
if(this.props.restaurantLog.status==true | | this.props.restaurantLog.status==false){
const{component:component,restaurantLog,…rest}=this.props;
返回(
{
返回restaurantLog.status
?
:
}} />
)
}
否则{
返回(等待)
}
}
}
函数mapStateToProps(状态,ownProps){
返回{
restaurantLog:state.logRestaurantState
}
}
功能图DispatchToprops(调度){
返回{
操作:bindActionCreators(操作、调度)
}
}
导出默认连接(
MapStateTops,
MapDispatchToProps
)(餐厅);
这是正确的语法和简单的路由(router元素必须有一个单子元素)。是否可以添加RestaurantauthoutreStaturanthoute的实现