簡體   English   中英

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

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

我嘗試過以下內容並且無法正常工作:

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

“Homepage”組件完全被忽略!

我正在使用react-router 1.0.0並反應0.13.3

首先,要做的最好的事情是升級到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'));

編輯:我在這里為你創建了一個例子。 克隆repo時,導航到plain-routes例子和npm install && npm start

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM