react-router如何使用Javascript对象配置索引路由?

时间:2015-11-13 02:57:23

标签: reactjs react-router

我尝试过以下内容并且无法正常工作:

const routeConfig = [
            {
                // path: '/',
                component: MyApp,
                indexRoute: {component: Homepage},
                childRoutes: routes
            }
        ];
        React.render(<Router history={history} routes={routeConfig} />, document.getElementById('content'));

完全忽略“主页”组件!

我正在使用react-router 1.0.0并做出反应0.13.3

1 个答案:

答案 0 :(得分:7)

首先,最好的办法是升级到1.0最终版(没有重大变化 - 只是错误修复)

请参阅下面的示例,了解如何使用IndexRoute的简单路由:

import React from 'react'
import { render } from 'react-dom';
import createBrowserHistory from 'history/lib/createBrowserHistory'
import { Router } from 'react-router'

const Home = () =>
  <h3>Home</h3>

const App = (props) =>
  <div>
    <h1>App</h1>
    <Navigation />
    {props.children}
  </div>

const routes = [
  {
      path: '/',
      component: App,
      indexRoute: {
        component: Home
      }
  }
]

const Root = () =>
  <Router history={createBrowserHistory()} routes={routes} />


render(<Root />, document.getElementById('root'));

修改:我为您here创建了一个示例。克隆回购时,请导航至plain-routes示例和npm install && npm start