我是React JS的初学者,我想为我的Dashboard开发基于路由器的反应导航。该模型如下:
我为尝试路由而创建的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'))
如何创建模型中显示的导航?
答案 0 :(得分:34)
是的,Daniel是正确的,但为了扩展他的答案,您的主要应用程序组件需要在其中包含导航栏组件。这样,当您渲染主应用程序(&#39; /&#39;路径下的任何页面)时,它也会显示导航栏。我猜你不希望你的登录页面显示导航栏,所以它不应该是一个嵌套的组件,而应该是它本身。所以你的路线最终会看起来像这样:
<Router>
<Route path="/" component={App}>
<Route path="page1" component={Page1} />
<Route path="page2" component={Page2} />
</Route>
<Route path="/login" component={Login} />
</Router>
其他组件看起来像这样:
var NavBar = React.createClass({
render() {
return (
<div>
<ul>
<a onClick={() => history.push('page1') }>Page 1</a>
<a onClick={() => history.push('page2') }>Page 2</a>
</ul>
</div>
)
}
});
var App = React.createClass({
render() {
return (
<div>
<NavBar />
<div>Other Content</div>
{this.props.children}
</div>
)
}
});
答案 1 :(得分:29)
注意接受完全没问题 - 但想添加一个版本4示例,因为它们足够不同。
import React from 'react';
import { Link } from 'react-router';
export default class Nav extends React.Component {
render() {
return (
<nav className="Nav">
<div className="Nav__container">
<Link to="/" className="Nav__brand">
<img src="logo.svg" className="Nav__logo" />
</Link>
<div className="Nav__right">
<ul className="Nav__item-wrapper">
<li className="Nav__item">
<Link className="Nav__link" to="/path1">Link 1</Link>
</li>
<li className="Nav__item">
<Link className="Nav__link" to="/path2">Link 2</Link>
</li>
<li className="Nav__item">
<Link className="Nav__link" to="/path3">Link 3</Link>
</li>
</ul>
</div>
</div>
</nav>
);
}
}
import React from 'react';
import { Link, Switch, Route } from 'react-router';
import Nav from './nav';
import Page1 from './page1';
import Page2 from './page2';
import Page3 from './page3';
export default class App extends React.Component {
render() {
return (
<div className="App">
<Router>
<div>
<Nav />
<Switch>
<Route exactly component={Page1} pattern="/path1" />
<Route exactly component={Page2} pattern="/path2" />
<Route exactly component={Page3} pattern="/path3" />
<Route component={Page404} />
</Switch>
</div>
</Router>
</div>
);
}
}
或者,如果您想要更加动态的导航,可以查看优秀的v4文档:https://reacttraining.com/react-router/web/example/sidebar
答案 2 :(得分:0)
对于@Chris 在功能组件中工作的答案。这是答案 React-router、router-dom 版本 v5.2.0
import React from 'react';
import { Link, Switch, Route } from 'react-router-dom';
import Header from './header';
import Footer from '.footer';
import Page1 from './page1';
import Page2 from './page2';
import Page3 from './page3';
import Loading from './Loading'; // a page to show spinner
const Dashboard = React.lazy(() => import('../pages/Dashboard'));
// if you have an admin dashboard to load lazily
const NavRoute = ({exact, path, component: Component}) => (
<Route exact={exact} path={path} render={(props) => (
<div>
<Header/>
<Component {...props}/>
<Footer/>
</div>
)}/>
)
function App() {
return (
<div>
<Suspense fallback={<Loading />}>
<Switch>
<NavRoute exact path="/" component={Landing} />
<Route exact path="/admin/dashboard" component={Dashboard}/>
<Route exact path="/" component={Login} />
<NavRoute exact path="/path1" component={Page1} />
<NavRoute exact path="/path2" component={Page2} />
<NavRoute component={Page404} />
</Switch>
</Suspense>
</div>
);
}
export default App;