Javascript 反应路由器:我不&x27;我不希望用户通过键入URL直接导航到页面,但只允许使用应用程序内的链接访问页面。

Javascript 反应路由器:我不&x27;我不希望用户通过键入URL直接导航到页面,但只允许使用应用程序内的链接访问页面。,javascript,reactjs,react-router,url-routing,Javascript,Reactjs,React Router,Url Routing,My Routes.js <Route path="/game-center" component={GameCenter} /> <Route path="/game-center/pickAndWin" component={PickAndWin} /> <Route path="/game-center/memory" component={Memory} /> <Route path="/game-center

My Routes.js

<Route path="/game-center" component={GameCenter} />
      <Route path="/game-center/pickAndWin" component={PickAndWin} />
      <Route path="/game-center/memory" component={Memory} />
      <Route path="/game-center/summary" component={GameSummary} />
    </Route>
  </Router>
当用户直接键入url“/game center/summary”时,不应允许他这样做,应将其发送回主页。 这在react路由器本身中是可能的吗?我想在我的整个应用程序中实现这一点。
我不希望用户通过键入URL直接导航到页面,而只使用应用程序内部的链接导航到页面。

您可以通过使用高阶组件来实现这一点。 例如,您可以在对用户进行身份验证时设置一个标志,然后将此HOC与react路由器中的指定组件连接

import React,{Component} from 'react';
import {connect} from 'react-redux';
export default function(ComposedComponent){
  class Authentication extends Component{
    static contextTypes = {
      router : React.PropTypes.object
    }
    componentWillMount(){
      if(!this.props.user){
        this.context.router.push('/');
      }
    }
    componentWillUpdate(nextProps){
      if(!nextProps.user){
          this.context.router.push('/');
      }
    }
    render(){
      return(<ComposedComponent {...this.props}/>);
    }
  } 
}
import React,{Component}来自'React';
从'react redux'导入{connect};
导出默认函数(ComposedComponent){
类身份验证扩展了组件{
静态上下文类型={
路由器:React.PropTypes.object
}
组件willmount(){
如果(!this.props.user){
this.context.router.push('/');
}
}
组件将更新(下一步){
如果(!nextrops.user){
this.context.router.push('/');
}
}
render(){
return();
}
} 
}
然后在你的路线上

  <Route path="home" component={requireAuth(Home)}></Route>

您可以通过使用高阶组件来实现这一点。 例如,您可以在对用户进行身份验证时设置一个标志,然后将此HOC与react路由器中的指定组件连接

import React,{Component} from 'react';
import {connect} from 'react-redux';
export default function(ComposedComponent){
  class Authentication extends Component{
    static contextTypes = {
      router : React.PropTypes.object
    }
    componentWillMount(){
      if(!this.props.user){
        this.context.router.push('/');
      }
    }
    componentWillUpdate(nextProps){
      if(!nextProps.user){
          this.context.router.push('/');
      }
    }
    render(){
      return(<ComposedComponent {...this.props}/>);
    }
  } 
}
import React,{Component}来自'React';
从'react redux'导入{connect};
导出默认函数(ComposedComponent){
类身份验证扩展了组件{
静态上下文类型={
路由器:React.PropTypes.object
}
组件willmount(){
如果(!this.props.user){
this.context.router.push('/');
}
}
组件将更新(下一步){
如果(!nextrops.user){
this.context.router.push('/');
}
}
render(){
return();
}
} 
}
然后在你的路线上

  <Route path="home" component={requireAuth(Home)}></Route>


我不关心身份验证。游戏中心组件通过点击我可以进入“/sumary”或游戏本身的卡片。我不希望用户直接在url中键入“/game center/summary”并转到摘要页面。实际上,您可以自定义上述代码,这样当用户尝试直接访问页面时,他会被踢回主页。如
this.context.router.push(“/”)
我不确定在上述代码中设置什么条件?我想确保他已经访问了游戏中心,并点击了按钮,将路线更改为摘要。如果他直接输入了url“/game center/summary”,他应该被重定向到主页。请详细说明一下。如果点击游戏中心,您可以简单地将
标志设置为
true
。然后在HOC中检查
标志是否为
true
,然后让用户通过else将其踢回主页。嘿!我想我明白你的意思了。非常感谢你的帮助。:)我不关心身份验证。游戏中心组件通过点击我可以进入“/sumary”或游戏本身的卡片。我不希望用户直接在url中键入“/game center/summary”并转到摘要页面。实际上,您可以自定义上述代码,这样当用户尝试直接访问页面时,他会被踢回主页。如
this.context.router.push(“/”)
我不确定在上述代码中设置什么条件?我想确保他已经访问了游戏中心,并点击了按钮,将路线更改为摘要。如果他直接输入了url“/game center/summary”,他应该被重定向到主页。请详细说明一下。如果点击游戏中心,您可以简单地将
标志设置为
true
。然后在HOC中检查
标志是否为
true
,然后让用户通过else将其踢回主页。嘿!我想我明白你的意思了。非常感谢你的帮助。:)