简体   繁体   English

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

[英]react-router how to configure the index route using Javascript object?

I've tried the following and it's not working: 我尝试过以下内容并且无法正常工作:

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

The "Homepage" component is ignored entirely! “Homepage”组件完全被忽略!

I'm using react-router 1.0.0 and react 0.13.3 我正在使用react-router 1.0.0并反应0.13.3

First of all the best thing to do is to upgrade to the 1.0 final (there are no breaking changes - just bug fixes) 首先,要做的最好的事情是升级到1.0最终版(没有重大变化 - 只是错误修复)

See the example below on how to use plain routes with IndexRoute : 请参阅下面的示例,了解如何使用带有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'));

Edit: I created an example for you here . 编辑:我在这里为你创建了一个例子。 When you clone the repo, navigate to plain-routes example and npm install && npm start . 克隆repo时,导航到plain-routes例子和npm install && npm start

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

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