简体   繁体   English

使用react-router将路由定向到特定组件

[英]Directing routes to specific components using react-router

Im want to use the following layout as the start screen of my page: 我想使用以下布局作为页面的开始屏幕:

-----------------------------
| View 1      | View 2      |
| LinkA LinkB | LinkC LinkD |
|             |             |
-----------------------------

When LinkA or B are clicked I want the routes to be shown in View 1 , and when LinkC or D are clicked I want the routes to be shown in View 2 . 单击LinkA or B ,我希望路由在View 1显示;单击LinkC或D时,我希望路由在View 2View 2

What I have right now is only a way to send multiple components to the App component to render View1 and View2, I dont know how to make specific routes to actually render inside them: 我现在所拥有的只是将多个组件发送到App组件以渲染View1和View2的一种方法,我不知道如何制作具体的路线以在其中实际渲染:

  <Route path="/" component={ App }>
    <IndexRoute components={{
        view1: View1,
        view2: View2
      }} />
  </Route>

What I know about react-router makes me think I want something more along the lines: 我对react-router的了解使我认为我需要更多的东西:

  <Route path="/" component={ App }>
    <Route component={View1} >
      <Route to="linkA" component={View1a}/>
      <Route to="linkB" component={View1b}/>
    <Route />
    <Route component={View2} >
      <Route to="linkC" component={View2a}/>
      <Route to="linkD" component={View2b}/>
    <Route />
  </Route>

Now my question is how to make the rendering work as I want and are there any tutorials showing this? 现在我的问题是如何根据需要使渲染工作,是否有任何教程显示此内容? Maybe im missing some basic stuff since I have not been able to find any examples like this and what I assume to be a basic layout for a page... 也许我错过了一些基本的东西,因为我无法找到这样的示例以及我认为是页面的基本布局的示例...

The only concern of a react-router config is to determine which component to populate this.props.children with based on the current url. react-router配置的唯一关注点是基于当前URL确定使用哪个组件填充this.props.children You do not specify which links are displayed in the config.. you do this in the component. 您不指定在配置中显示哪些链接。在组件中执行此操作。

In your question you say you want the start screen to contain two views. 在问题中,您说您希望“开始”屏幕包含两个视图。 Maybe you mean two components? 也许您是指两个组成部分? Every URL is responsible for one view. 每个URL负责一个视图。

With a config that looks like: 使用如下配置:

<Route path="/" component={ App }>
  <Route path="view1" component={ View1 }>
    <Route path="a" component={ View1a } />
  <Route>
  <Route path="view2" component={ View2 } />
</Route>

When the url is / , the App component's this.props.children will be empty. 当url为/App组件的this.props.children将为空。 If you use IndexRoute it will use the component specified there. 如果使用IndexRoute ,它将使用在那里指定的组件。

When the url is /view1 , the App component's this.props.children will render the View1 component. 当url为/view1App组件的this.props.children将呈现View1组件。 Same thing for /view2 and View2 . 对于同样的事情/view2View2

When the url is /view1/a , the App component's this.props.children will be View1 , and View1 's this.props.children will be View1a 当url为/view1/aApp组件的this.props.children将为View1 ,而View1this.props.children将为View1a

So if you want to display different links based on the current view, use different links in your View1 and View2 components. 因此,如果要基于当前视图显示不同的链接,请在View1View2组件中使用不同的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM