我目前正在努力使用react router v4嵌套路由.

最接近的例子是

我想把我的应用程序分成两个不同的部分.

一个前端和一个管理区.

我在想这样的事情:

<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} />

前端的布局和样式与管理区不同.所以在首页中,关于回家的路由,所以其中一条应该是子路由.

/home应该呈现在Frontpage组件中,/admin/home应该呈现在后端组件中.

我try 了一些变化,但我总是不点击/home或/admin/home.

Edit - 19.04.2017

因为这篇文章现在有很多观点,我用最终的解决方案更新了它.我希望它能帮助别人.

Edit - 08.05.2017

已删除旧解决方案

Final solution:

这是我现在使用的最后一个解决方案.此示例还有一个全局错误组件,类似于传统的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;

推荐答案

In react-router-v4 you don't nest 100. Instead, you put them inside another 101.


例如

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

应该成为

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

使用

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

这是一个basic example从react 路由documentation直接.

Javascript相关问答推荐

在JavaScript中打开和关闭弹出窗口

序列查找器功能应用默认值而不是读取响应

vscode扩展-webView Panel按钮不起任何作用

Plotly热图:在矩形上zoom 后将zoom 区域居中

Angular:动画不启动

我可以从React中的出口从主布局调用一个处理程序函数吗?

为什么当我解析一个promise时,输出处于挂起状态?

函数返回与输入对象具有相同键的对象

为什么!逗号和空格不会作为输出返回,如果它在参数上?

我的服务工作器没有连接到我的Chrome扩展中的内容脚本.我该怎么解决这个问题?

如何在输入元素中附加一个属性为checkbox?

创建以键值对为有效负载的Redux Reducer时,基于键的类型检测

如果一个字符串前面有点、空格或无字符串,后面有空格、连字符或无字符串,则匹配正则表达式

如何在和IF语句中使用||和&;&;?

用于部分字符串的JavaScript数组搜索

postman 预请求中的hmac/sha256内标识-从js示例转换

如果NetSuite中为空,则限制筛选

不协调嵌入图片

在Java脚本中构建接口的对象

调用特定数组索引时,为什么类型脚本不判断未定义