【问题标题】:React router displaying content at root route反应路由器在根路由显示内容
【发布时间】:2016-11-28 05:56:09
【问题描述】:

我正在将 React 路由器用于 SPA,但我不确定如何在索引路由“/”上显示一些内容。

我的顶级路由器配置只是:

<Route path="/" component={App}>
    // ...
</Route>

顶级组件呈现页眉/页脚和其他常见元素:

<nav>
    // ...
</nav>

{children}

<nav className="footer">
    // ...
</nav>

子路由工作正常,它们在 App 组件中呈现。但是,如何在根路由处呈现一些内容,使其不仅仅是一个空白页面?

【问题讨论】:

    标签: reactjs react-router


    【解决方案1】:

    说你想为&lt;App&gt;中的根路由服务的内容是&lt;Home&gt;,下面引用自react-routeguide

    <Router>
      <Route path="/" component={App}>
        <IndexRoute component={Home}/>
        <Route path="accounts" component={Accounts}/>
        <Route path="statements" component={Statements}/>
      </Route>
    </Router>
    

    这就是&lt;App&gt; 的样子。

    const App = React.createClass({
        render() {
            return {this.props.children || <Home/>};
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2020-01-21
      • 2019-01-07
      • 2019-10-24
      • 1970-01-01
      • 2020-03-13
      • 1970-01-01
      • 2020-10-27
      • 2020-07-04
      • 2021-11-06
      相关资源
      最近更新 更多