我是 React JS 的初学者,想为我的仪表板开发一个基于反应路由器的导航。样机如下:
我为尝试路由而创建的 app.js 代码如下:
import React from 'react'
import { render } from 'react-dom'
import { Router, Route, Link } from 'react-router'
import Login from './components/Login.js';
const App = React.createClass({
render() {
return (
<div>
<h1>App</h1>
<ul>
<li><Link to="/login">Login</Link></li>
<li><Link to="/inbox">Inbox</Link></li>
</ul>
{this.props.children}
</div>
)
}
})
render((
<li>
<Router>
<Route path="/" component={App}>
<Route path="login" component={Login} />
</Route>
</Router>
</li>
), document.getElementById('placeholder'))
如何创建模型中所示的导航?
原文由 cucucool 发布,翻译遵循 CC BY-SA 4.0 许可协议
是的,Daniel 是正确的,但要扩展他的答案,您的主要应用程序组件需要在其中包含一个导航栏组件。这样,当您渲染主应用程序(“/”路径下的任何页面)时,它也会显示导航栏。我猜你不希望你的登录页面显示导航栏,所以它不应该是一个嵌套组件,而应该是单独的。所以你的路线最终看起来像这样:
其他组件看起来像这样: