反应嵌套路由

时间:2016-11-17 22:10:23

标签: javascript reactjs react-router

我正在学习React,我在想如何创造动态路线 比如Angular中的部分页面。

这是我的顶级App组件

import React from 'react';
import Header from '../common/Header';

export default class App extends React.Component {

  render() {
    return (
      <div>
        <Header/>
        //Render partial pages here e.g /dashboard, /users 
      </div>
    );
  }
}

My Route看起来像这样:

import React from 'react';
import {render} from 'react-dom';
import {Router, IndexRoute, Route, browserHistory} from 'react-router';
import App from './app/components/containers/App';
import Dashboard from './app/components/containers/Dashboard';
import Ecosystem from './app/components/ecosystem/Ecosystem';

import './assets/plugins/morris/morris.css';
import './assets/css/bootstrap.min.css';
import './assets/css/core.css';
import './assets/css/components.css';
import './assets/css/icons.css';
import './assets/css/pages.css';
import './assets/css/menu.css';
import './assets/css/responsive.css';

render(
  <Router history={browserHistory}>

    <Route component={App}>
      <IndexRoute component={Dashboard}/>
      <Route path="/ecosystem/:name" component={Ecosystem}/>
      <Route path="*" component={Dashboard}/>
    </Route>
  </Router>,
  document.getElementById('root')
);

请问好吗?

1 个答案:

答案 0 :(得分:2)

这有用吗?

import React from 'react';
import Header from '../common/Header';

class App extends React.Component {

  render() {
    return (
      <div>
        <Header/>
        {this.props.children}
      </div>
    );
  }
}

App.propTypes = {
    children: React.PropTypes.node,
};

export default App;