gpt4 book ai didi

javascript - React 嵌套路由

转载 作者:行者123 更新时间:2023-12-02 13:56:32 25 4
gpt4 key购买 nike

我正在学习 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>
);
}
}

我的路线如下所示:

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')
);

请问有什么想法吗?

最佳答案

这有效吗?

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;

关于javascript - React 嵌套路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40665952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com