[英]React-Router-DOM: unable to render Link in the same page (component)
consider the following example, I have a login page and an admin page.考虑以下示例,我有一个登录页面和一个管理页面。 After logging in we will be redirected to admin page.
登录后,我们将被重定向到管理页面。
The admin page is shown as follows.
管理页面如下所示。
Desired behaviour : To render cars component in the admin component itself期望的行为:在管理组件本身中呈现汽车组件
Actual behaviour : On clicking cars or bikes component they are being rendered on a different page.实际行为:单击汽车或自行车组件时,它们将呈现在不同的页面上。
code is as follows代码如下
App.js应用程序.js
//imports here
function App() {
return(
<Router>
<Switch>
<Route exact path="/" component={Login} />
<Route exact path="/admin" component={Admin} />
<Route exact path="/cars" component={Cars} />
<Route exact path="/bikes" component={Bikes} />
</Switch>
</Router>
);
}
Admin.js管理.js
//imports here
const Admin = () => {
return (
<Fragment>
<div className="is-flex">
<Sidebar />
<Navbar />
</div>
</Fragment>
);
};
navbar.js导航栏.js
// imports here
const Sidebar = () => {
return (
<aside className="aside">
<p className="menu-label">Test Routes</p>
<ul className="menu-list">
<li>
<Link to="/cars">Cars</Link>
</li>
<li>
<Link to="/bikes">Bikes</Link>
</li>
</ul>
</aside>
);
};
Using react-router-dom ^5.1.2 Tried this but not able to understand what I missed?使用 react-router-dom ^5.1.2 尝试过这个但无法理解我错过了什么? how to solve this problem?
如何解决这个问题呢?
Move your default route to the bottom of the stack.将默认路由移至堆栈底部。 ie
IE
function App() {
return(
<Router>
<Switch>
<Route path="/admin" component={Admin} />
<Route path="/cars" component={Cars} />
<Route path="/bikes" component={Bikes} />
<Route exact path="/" component={Login} />
</Switch>
</Router>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.