简体   繁体   English

react-router-dom v4 中的多个嵌套路由

[英]Multiple Nested Routes in react-router-dom v4

I need multiple nested routes in react-router-dom我需要 react-router-dom 中的多个嵌套路由

I am using v4 of react-router-dom我正在使用 react-router-dom 的 v4

I've got my我有我的

import { BrowserRouter as Router, Route } from 'react-router-dom';

and I need the components to render like so我需要组件像这样渲染

--- Login
--- Home
    --- Page 1
    --- Page 2
    --- Page 3
--- About
--- etc

The Home component contains a Header component that is common to Page1, Page2, and, Page3 components, but is not present in Login and About. Home 组件包含一个 Header 组件,它对于 Page1、Page2 和 Page3 组件是通用的,但在 Login 和 About 中不存在。

My js code reads like so我的 js 代码是这样读的

<Router>
    <div>
        <Route path='/login' component={Login} />
        <Home>
            <Route path='/page1' component={Page1} />
            <Route path='/page2' component={Page2} />
            <Route path='/page3' component={Page3} />
        </Home>
        <Route path='/about' component={About} />
    </div>
</Router>

I expect the Login component to show only on /login When I request for /page1, /page2, /page3, they should contain the Home component and that page's content respectively.我希望登录组件只显示在 /login 当我请求 /page1、/page2、/page3 时,它们应该分别包含 Home 组件和该页面的内容。

What I get instead is the Login component rendered and below that Page1's component is rendered.我得到的是呈现的登录组件,并在页面 1 的组件下方呈现。

I'm pretty sure that I'm missing something very trivial or making a really silly mistake somewhere, and would appreciate all the help I could get.我很确定我遗漏了一些非常微不足道的东西或在某处犯了一个非常愚蠢的错误,并感谢我能得到的所有帮助。 I've been stuck with this for the last two days.过去两天我一直被这个问题困扰。

Use the url/path match obtained from props this.props.match.path to get the path that is set to a component.使用从 props this.props.match.path获取的 url/path 匹配来获取设置到组件的路径。

Define your main routes as below定义您的主要路线如下

<Router>
  <div>
    <Route exact path="/" component={DummyIndex} /> {/* Note-1 */}
    <Route path="/login" component={Login} />
    <Route path="/home" component={Home} />
    <Route path="/about" component={About} />
    <Route path="/etc" component={Etc} />
  </div>
</Router>

Then in Home Component, define your routes然后在Home组件中,定义你的路由

class Home extends Component {
  render() {
    return <div>
      <Route exact path={this.props.match.path} component={HomeDefault} />
      <Route path={`${this.props.match.path}/one`} component={HomePageOne} />
      <Route path={`${this.props.match.path}/two`} component={HomePageTwo} />
    </div>
  }
}

The defined routes are as below定义的路由如下

  • /login /登录
  • /home /家
  • /home/one /家/一
  • /home/two /家/二
  • /about /关于
  • /etc /等等

If you want to nest routes further in HomePageOne like /home/one/a and /home/one/b , you can proceed the same approach.如果您想在HomePageOne进一步嵌套路由,例如/home/one/a/home/one/b ,您可以继续使用相同的方法。

Note-1: If you don't want further nesting, just set the route with prop exact .注意 1:如果您不想进一步嵌套,只需使用 prop exact设置路由即可。

EDIT (May 15, 2017)编辑(2017 年 5 月 15 日)

Initially, I've used props.match.url and now I changed it to props.match.path .最初,我使用了props.match.url ,现在我将其更改为props.match.path

We can use props.match.path instead of props.match.url in Route's path so that if you use path params in top level routes, you can get it in inner (nested) routes via props.match.params .我们可以在路由的路径中使用props.match.path而不是props.match.url ,这样如果你在顶级路由中使用路径参数,你可以通过props.match.params在内部(嵌套)路由中获取它。

If you don't you any path params, props.match.url is enough如果你没有任何路径参数, props.match.url就足够了

Use Switch component in router v4在路由器 v4 中使用 Switch 组件

<Router>
<Switch>
  <Route path='/login' component={Login} />
  <Route path='/about' component={About} />
  <Home>
    <Route component={({ match }) =>
      <div>
        <Route path='/page1' component={Page1} />
        <Route path='/page2' component={Page2} />
        <Route path='/page3' component={Page3} />
      </div>
    }/>
  </Home>
</Switch>

export default class Home extends Component {
render() {
    return (
      <div className="Home">
          { this.props.children }
      </div>
    )
  }
}

I think this code shows the basic idea of using component.我认为这段代码展示了使用组件的基本思想。

This week I had the same problem, I think the project is passing for time of confusion because all the documentation, examples and videos are for the previous versions and the docs for the version 4 are confusing本周我遇到了同样的问题,我认为该项目已经过去了,因为所有的文档、示例和视频都是针对先前版本的,而版本 4 的文档令人困惑
This is how I get the things done, let me know if this help这就是我完成工作的方式,如果这有帮助,请告诉我

import React, { Component } from 'react';

import { BrowserRouter, Route, Switch } from 'react-router-dom';

import Root from './Root';
import Home from './Home';
import About from './About';

import './App.css';

class App extends Component {
    render() {
        return (
            <BrowserRouter>
                <div>
                    <Root>
                       <Switch>
                            <Route exact path="/" component={Home} />
                            <Route path="/home" component={Home} />
                            <Route path="/about" component={About} />
                        </Switch>
                    </Root>
                </div>
            </BrowserRouter>
        );
    }
}

export default App;

Move all childs routes to parent component and extend route like below.将所有子路由移动到父组件并扩展路由,如下所示。
<Route path={`${match.url}/keyword`} component={Topic}/>
also check react router training还检查反应路由器培训

Use Like the following:使用如下:

class App extends Component {
  render() {
    return (
      <BrowserRouter>
        <Link to='/create'> Create </Link>
        <Link to='/ExpenseDashboard'> Expense Dashboard </Link>
        <Switch>
          <Route path='/ExpenseDashboard' component={ExpenseDashboard} />
          <Route path='/create' component={AddExpensePage} />
          <Route path='/Edit' component={EditPage} />
          <Route path='/Help' component={HelpPage} />
          <Route component={NoMatch} />
        </Switch>
      </BrowserRouter>
    );
  }
}

See more @ Switch on GitHub查看更多 @ 在 GitHub 上切换

I had the same problem and I solved it like this我有同样的问题,我是这样解决的

<BrowserRouter>
          <UserAuthProvider>
            <Root>
              <Switch>
                <GuardRoute type="public" exact path="/" component={Login} />
                <GuardRoute type="private" exact path="/home" component={Home} />
                <GuardRoute
                  type="private"
                  exact
                  path="/home/mascotas"
                  component={Mascotas}
                />

              </Switch>
            </Root>
          </UserAuthProvider>
        </BrowserRouter>

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

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