Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

javascript - 使用react路由器v4 / v5的嵌套路由(Nested routes with react router v4 / v5)

I am currently struggling with nesting routes using react router v4.

(我目前正在使用react router v4进行嵌套路由。)

The closest example was the route config in the React-Router v4 Documentation .

(最接近的示例是React-Router v4文档中的路由配置。)

I want to split my app in 2 different parts.

(我想将我的应用程序拆分为两个不同的部分。)

A frontend and an admin area.

(前端和管理区域。)

I was thinking about something like this:

(我在考虑这样的事情:)

<Match pattern="/" component={Frontpage}>
  <Match pattern="/home" component={HomePage} />
  <Match pattern="/about" component={AboutPage} />
</Match>
<Match pattern="/admin" component={Backend}>
  <Match pattern="/home" component={Dashboard} />
  <Match pattern="/users" component={UserPage} />
</Match>
<Miss component={NotFoundPage} />

The frontend has a different layout and style than the admin area.

(前端具有与管理区域不同的布局和样式。)

So within the frontpage the route home, about and so one should be the child routes.

(因此,在首页的路线回家,约一个应该是儿童路线。)

/home should be rendered into the Frontpage component and /admin/home should be rendered within the Backend component.

(/ home应该呈现在Frontpage组件中,而/ admin / home应该在Backend组件中呈现。)

I tried some variations but I always ended in not hitting /home or /admin/home.

(我尝试了一些变化,但我总是在没有击中/ home或/ admin / home。)

Edit - 19.04.2017

(编辑 - 19.04.2017)

Because this post has a lot of views right now I updated it with the final solution.

(因为这篇文章现在有很多观点我用最终解决方案更新了它。)

I hope it helps someone.

(我希望它对某人有帮助。)

Edit - 08.05.2017

(编辑 - 08.05.2017)

Removed old solutions

(删除旧解决方案)

Final solution:

(最终解决方案)

This is the final solution I am using right now.

(这是我现在使用的最终解决方案。)

This example also has a global error component like a traditional 404 page.

(此示例还有一个全局错误组件,如传统的404页面。)

import React, { Component } from 'react';
import { Switch, Route, Redirect, Link } from 'react-router-dom';

const Home = () => <div><h1>Home</h1></div>;
const User = () => <div><h1>User</h1></div>;
const Error = () => <div><h1>Error</h1></div>

const Frontend = props => {
  console.log('Frontend');
  return (
    <div>
      <h2>Frontend</h2>
      <p><Link to="/">Root</Link></p>
      <p><Link to="/user">User</Link></p>
      <p><Link to="/admin">Backend</Link></p>
      <p><Link to="/the-route-is-swiggity-swoute">Swiggity swooty</Link></p>
      <Switch>
        <Route exact path='/' component={Home}/>
        <Route path='/user' component={User}/>
        <Redirect to={{
          state: { error: true }
        }} />
      </Switch>
      <footer>Bottom</footer>
    </div>
  );
}

const Backend = props => {
  console.log('Backend');
  return (
    <div>
      <h2>Backend</h2>
      <p><Link to="/admin">Root</Link></p>
      <p><Link to="/admin/user">User</Link></p>
      <p><Link to="/">Frontend</Link></p>
      <p><Link to="/admin/the-route-is-swiggity-swoute">Swiggity swooty</Link></p>
      <Switch>
        <Route exact path='/admin' component={Home}/>
        <Route path='/admin/user' component={User}/>
        <Redirect to={{
          state: { error: true }
        }} />
      </Switch>
      <footer>Bottom</footer>
    </div>
  );
}

class GlobalErrorSwitch extends Component {
  previousLocation = this.props.location

  componentWillUpdate(nextProps) {
    const { location } = this.props;

    if (nextProps.history.action !== 'POP'
      && (!location.state || !location.state.error)) {
        this.previousLocation = this.props.location
    };
  }

  render() {
    const { location } = this.props;
    const isError = !!(
      location.state &&
      location.state.error &&
      this.previousLocation !== location // not initial render
    )

    return (
      <div>
        {          
          isError
          ? <Route component={Error} />
          : <Switch location={isError ? this.previousLocation : location}>
              <Route path="/admin" component={Backend} />
              <Route path="/" component={Frontend} />
            </Switch>}
      </div>
    )
  }
}

class App extends Component {
  render() {
    return <Route component={GlobalErrorSwitch} />
  }
}

export default App;
  ask by datoml translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In react-router-v4 you don't nest <Routes /> .

(在react-router-v4中,您不嵌套<Routes />)

Instead, you put them inside another <Component /> .

(相反,您将它们放在另一个<Component />)


For instance

(例如)

<Route path='/topics' component={Topics}>
  <Route path='/topics/:topicId' component={Topic} />
</Route>

should become

(应该成为)

<Route path='/topics' component={Topics} />

with

(同)

const Topics = ({ match }) => (
  <div>
    <h2>Topics</h2>
    <Link to={`${match.url}/exampleTopicId`}>
      Example topic
    </Link>
    <Route path={`${match.url}/:topicId`} component={Topic}/>
  </div>
) 

Here is a basic example straight from the react-router documentation .

(这是一个直接来自react-router 文档基本示例 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...